Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>you're using innodb but not taking full advantage of your innodb clustered index (primary key) as it looks like your typical query is of the form:</p> <pre><code>select &lt;fields&gt; from &lt;table&gt; where user_id = x and &lt;datefield&gt; between y and z </code></pre> <p>not</p> <pre><code>select &lt;fields&gt; from &lt;table&gt; where id = x </code></pre> <p>the following article should help you optimise your table design for your query.</p> <p><a href="http://www.xaprb.com/blog/2006/07/04/how-to-exploit-mysql-index-optimizations/" rel="nofollow">http://www.xaprb.com/blog/2006/07/04/how-to-exploit-mysql-index-optimizations/</a></p> <p>If you understand the article correctly you should find youself with something like the following:</p> <pre><code>drop table if exists user_log; create table user_log ( user_id int unsigned not null, created_date datetime not null, log_type_id tinyint unsigned not null default 0, -- 1 byte vs varchar(10) ... ... primary key (user_id, created_date, log_type_id) ) engine=innodb; </code></pre> <p>Here's some query performance stats from the above design:</p> <p><strong>Counts</strong></p> <pre><code>select count(*) as counter from user_log counter ======= 37770394 select count(*) as counter from user_log where created_date between '2010-09-01 00:00:00' and '2010-11-30 00:00:00' counter ======= 35547897 </code></pre> <p><strong>User and date based queries (all queries run with cold buffers)</strong></p> <pre><code>select count(*) as counter from user_log where user_id = 4755 counter ======= 7624 runtime = 0.215 secs select count(*) as counter from user_log where user_id = 4755 and created_date between '2010-09-01 00:00:00' and '2010-11-30 00:00:00' counter ======= 7404 runtime = 0.015 secs select user_id, created_date, count(*) as counter from user_log where user_id = 4755 and created_date between '2010-09-01 00:00:00' and '2010-11-30 00:00:00' group by user_id, created_date order by counter desc limit 10; runtime = 0.031 secs </code></pre> <p>Hope this helps :)</p>
 

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