Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat indexes can be used to improve this query?
    primarykey
    data
    text
    <p>This query selects all the unique visitor sessions in a certain date range:</p> <pre><code>select distinct(accessid) from accesslog where date &gt; '2009-09-01' </code></pre> <p>I have indexes on the following fields:</p> <ul> <li>accessid</li> <li>date</li> <li>some other fields </li> </ul> <p>Here's what explain looks like:</p> <pre><code>mysql&gt; explain select distinct(accessid) from accesslog where date &gt; '2009-09-01'; +----+-------------+-----------+-------+----------------------+------+---------+------+-------+------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-----------+-------+----------------------+------+---------+------+-------+------------------------------+ | 1 | SIMPLE | accesslog | range | date,dateurl,dateaff | date | 3 | NULL | 64623 | Using where; Using temporary | +----+-------------+-----------+-------+----------------------+------+---------+------+-------+------------------------------+ mysql&gt; explain select distinct(accessid) from accesslog; +----+-------------+-----------+-------+---------------+----------+---------+------+---------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-----------+-------+---------------+----------+---------+------+---------+-------------+ | 1 | SIMPLE | accesslog | index | NULL | accessid | 257 | NULL | 1460253 | Using index | +----+-------------+-----------+-------+---------------+----------+---------+------+---------+-------------+ </code></pre> <p>Why doesn't the query with the date clause use the accessid index?</p> <p>Are there any other indexes I can use to speed up queries for distinct accessid's in certain date spans?</p> <p><b>Edit - Resolution</b></p> <p>Reducing column width on <code>accessid</code> from varchar 255 to char 32 improved query time by ~75%.</p> <p>Adding a <code>date+accessid</code> index had no effect on query time.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload