开发者

MySQL Query Cost

Can someone tell me the theoretical difference in the e开发者_如何学JAVAxpensive of the following queries?

  • SELECT * FROM posts WHERE category IN(1)
  • SELECT * FROM posts WHERE category IN(1,2)
  • SELECT * FROM posts WHERE category IN(1,2,3)

... etc ...

Thanks.


Theoretically, if you had an index on category AND the table was sizable enough AND the index is selective, then

Cost for 1st as baseline = N
Cost of 2nd = 2 x N
Cost of 3rd = 3 x N

If there is no index or the index is not selective enough (e.g. category=1 covers 10% of the table) then it will perform a table scan. If the table is small, it will not even bother to use the index. In which case

Cost of 1st as baseline = N + X
Cost of 2nd = N + 2X
Cost of 3rd = N + 3X

The reason is that a full table scan is cost N. During the scan, it will use X time pick out the entire row (select *) so there is a little bit more involved in processing more categories, but not so significant as to affect the baseline time required to scan the table.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜