select top 20 * from Table a,Table2 b
where a.bid = b.id
and b.id in (select bid from Table3 c where id = 30)
// order by id descselect top 20 * from Table1 a,Table2 b
where a.bid = b.id
and b.id in (select bid from Table3 c where id = 30)
order by a.id desc2条语句中查询的Table2和Table3是同一个表.
Table和Table1的数据量差不多都在200W左右,数据表结构也差不多,索引都一样.
现在2条语句都需要用倒序排序.
问题就是如果第一条不用倒序desc就很快,并且用到了索引.用了desc就慢,没有用到索引.
第二条用了倒序就很快,用到了索引.没有desc就很慢,并且没用到索引.
一直找不到原因,麻烦高手给点建议,非常感谢.
where a.bid = b.id
and b.id in (select bid from Table3 c where id = 30)
// order by id descselect top 20 * from Table1 a,Table2 b
where a.bid = b.id
and b.id in (select bid from Table3 c where id = 30)
order by a.id desc2条语句中查询的Table2和Table3是同一个表.
Table和Table1的数据量差不多都在200W左右,数据表结构也差不多,索引都一样.
现在2条语句都需要用倒序排序.
问题就是如果第一条不用倒序desc就很快,并且用到了索引.用了desc就慢,没有用到索引.
第二条用了倒序就很快,用到了索引.没有desc就很慢,并且没用到索引.
一直找不到原因,麻烦高手给点建议,非常感谢.
SQL Select语句完整的执行顺序: 1、from子句组装来自不同数据源的数据;
2、where子句基于指定的条件对记录行进行筛选;
3、group by子句将数据划分为多个分组;
4、使用聚集函数进行计算;
5、使用having子句筛选分组;
6、计算所有的表达式;
7、使用order by对结果集进行排序。
另外:一个查询语句各个部分的执行顺序:
--8) SELECT (9) DISTINCT (11) <TOP_specification> <select_list>
--(1) FROM <left_table>
--(3) <join_type> JOIN <right_table>
--(2) ON <join_condition>
--(4) WHERE <where_condition>
--(5) GROUP BY <group_by_list>
--(6) WITH {CUBE | ROLLUP}
--(7) HAVING <having_condition>
--(10) ORDER BY <order_by_list>
where a.bid = b.id
and b.id in (select bid from Table3 c where id = 30)
// order by id desc你这order by id desc 是哪个表的id, 你是没有加上表名还是提问的时候忘记写了?
如果执行的时候没有加上, 加上试试看, 比如a.id, b.id
where a.bid = b.id
and exists (select * from Table3 c where id = 30 and c.bid = b.id)
order by a.id desc