作者tt0311 (turtle)
看板Database
标题[SQL ] 查询条件的顺序跟查询速度的关系?
时间Thu Sep 30 23:54:39 2010
我有三个条件需要比对
并且我有对这三个条件做index
index做的顺序是A, B, C
请问一下
为什麽
1.
select * from TABLE where B < 10 and C > 50 and A in
(select A from TABLE where B < 20 and C > 40)
2.
select * from TABLE where A in
(select A from TABLE where B < 20 and C > 40) and
B < 10 and C > 50
1的速度会比2来的快
记得看到的资料是说应该要照index的顺序来排会比较好
另外还有个问题是
如果没有作index的话
我的三个条件分别是用 "=", ">或<", "like" 来作比对
哪一种比对排在前面会对查询速度有影响吗
谢谢回答
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 123.194.102.114
1F:推 ThreeDay0905:子查询一定比较慢 WHERE的规则是依照顺序筛减资料 10/01 00:17
2F:→ tt0311:因为1跟2都有子查询,比较好奇的是为什麽1比2快 10/01 00:23
3F:推 ThreeDay0905:第二个所有资料都进子查询 第一个只有B<10&C>50才进 10/01 00:24
4F:→ tt0311:原来如此 谢谢 10/01 00:30