1、where 子句中的函数

    在做查询是,很多情况下where 查询后会将表中的某一列包装在函数中,再做查询,比如

    select * from smart..tb_product where substring(name,1,2)='cp'

    这样做会使查询优化器看不到该列的索引,只能进行全表扫描。在实际的应用中应该使用其他方法尽量避免把列包装在函数中。上面的例子可以换成

    select * from smart..tb_product where name like 'cp%'

    性能将大大优化。


2、查询表中的记录

    通常情况下,我们这样查询:

    select count(*) from smart..tb_product

    还有一种更有效率的查询方法:

    select sum(row_count) 'TotalRows'
      from sys.dm_db_partition_stats
      where object_id=object_id('smart..tb_product')
      and index_id<=1


    当然如果查询数据库中所有表的行,有以下查询方法:

    select so.name 'TableName',so.type,sum(row_count) 'ToalRows'
    from sys.dm_db_partition_stats ps
    inner join  sys.objects so on ps.[object_id]=so.[object_id]
    where index_id<=1 and so.[type]='U'
    group by so.name,so.type
    order by sum(row_count) desc