下面想问的是一个查询耗时的问题. 如下的SQL语句都是建立在数据量很大可能几千万或上亿条. 而且表里好几个字段是text类型的字段想请教大家这些语句耗时有些什么区别.表结构如下. 只写几个主要字段tbale id int 主键 主键是天然的索引就不说了
createdate date (有建索引和不建索引)
其他字段就不写了查询语句如下:
语句1:select * from table order by id desc 和select * from table order by createdate(有给这个字段建索引时和没建) desc ,id desc order by 两个条件的语句速度快还是一个条件的??为什么呢。语句2:select * from table where id = id 和 select * from table where id = id order by id desc 和select * from table where id = id order by createdate desc 这3条语句速度快是一样快还是有区别。 谁更快?也许这些语句耗时之间的差距很微乎其微大家也不太在意,呵呵 我只是了解得更多一点,谢谢。希望大家能给我个答案!
createdate date (有建索引和不建索引)
其他字段就不写了查询语句如下:
语句1:select * from table order by id desc 和select * from table order by createdate(有给这个字段建索引时和没建) desc ,id desc order by 两个条件的语句速度快还是一个条件的??为什么呢。语句2:select * from table where id = id 和 select * from table where id = id order by id desc 和select * from table where id = id order by createdate desc 这3条语句速度快是一样快还是有区别。 谁更快?也许这些语句耗时之间的差距很微乎其微大家也不太在意,呵呵 我只是了解得更多一点,谢谢。希望大家能给我个答案!
set statistics time on
来测试一下呵呵
语句1: order by 一个字段和 order by 两个字段的时候哪个会更快一些。还是说没区别!
语句2:有条件语句查询时比如:where id = @id 后面 加上order by的时候会更快的查询出 id=@id的这条记录还是不加。 当然可能where 的时候不是ID这样的主键,是别的字段切没有索引。
2. 这个和order by应该没关系...毕竟是先有结果集,然后再排序的