This is a fun question I've been wanting to test for some time. How much overhead does a trivial WHERE clause add to a MySQL query? To find out, I set my InnoDB buffer pool to 256MB and created a table that's large enough to test, but small enough to fit wholly in memory:
I repeated the last statement until it got slow enough that I thought I could do a reasonable test; at this point there were 8388608 rows. I then optimized the table and restarted MySQL. The table ended up at 237MB.
Now let's see how long a table scan with no WHERE clause takes:
I repeated this ten times and averaged the time: 5.01 seconds, ranging from 4.83 to 5.17 seconds. Next I ran it with a trivial WHERE clause:
(Pop quiz: do I need SQL_NO_CACHE on this query?) The average execution time for this query is 7.96, ranging from 7.65 to 8.15 seconds. So apparently the overhead of the WHERE clause is about 2.95 seconds, or 351 nanoseconds per row. It doesn't seem like much per row, but it adds up to about an extra 60% cost for the query. If I add another WHERE clause,
The average time is 9.39 seconds, or an added 87% overhead.
It would be interesting, in an academic kind of way, to test different data types and various complexities of WHERE clauses, but I'm not really interested enough to spend much time on it. I just wondered whether the WHERE clause would even be noticeable. This test doesn't really reveal anything you can do to make your queries faster -- you usually can't optimize the WHERE clause itself, only the way that the MySQL optimizer chooses to apply the clause to indexes. (However, as I found out some time ago, some date functions are faster than others. That would be interesting to look into more.)
Entry posted by Baron Schwartz | 9 comments
最近《视觉电压》正在中信泰富广场中庭举办(上海市南京西路1168号 对公众免费开放);
这里摘录一组数据:2005年全球发电量 18,200 * 10亿千瓦时 (而世界上有20亿人过着没有电的生活),其中
火电 12,100 * 10亿千瓦时 66%
水电 2,900 * 10亿千瓦时 16%
核电 2,800 * 10亿千瓦时 15%
其他 400 * 10亿千瓦时 2%
另外介绍的几个环保做法:
由于豆瓣服务器所在的网络出现问题,豆瓣的服务从今天上午11点起中断。
我们的工程师正在全力解决中,请大家稍等。
给大家造成了不便,豆瓣团队深感不安和歉意。
十一月 2008 | ||||||
一 | 二 | 三 | 四 | 五 | 六 | 日 |
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |