在数据库查询过程中经常会遇到语句查询慢,执行效率低的情况,以后归纳总结如下:
1. 查询存在A表不存在B表的语句
select * from a where (select count(1) from b where b.id=a.id)=0
原来效率低的查询为:
select * from a where a.id not in (select id from b )
注意:如果在查询的时候B表中的值有null的时候,通过in 和not in 的查询最后会没有结果返回。所以此种方法一般大家都不推荐。
另一种改进查询方法为 :
select * from a left join b on a.id=b.id where b.id is null
2.Oracle中快速查看表的行数:
select t.table_name,t.num_rows from user_tables t
3.SQL SERVER中快速查看表的行数:
select schema_name(t.schema_id) as [Schema], t.name as TableName,i.rows as [RowCount]
from sys.tables as t, sysindexes as i
where t.object_id = i.id and i.indid <=1
AND t.name = '表名'