首页 > 数据库技术 > 详细

T-SQL 优化

时间:2015-09-23 11:59:30      阅读:188      评论:0      收藏:0      [点我收藏+]

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

本文出自 “沧海拾贝” 博客,请务必保留此出处http://angeljing.blog.51cto.com/7370594/1697341

T-SQL 优化

原文:http://angeljing.blog.51cto.com/7370594/1697341

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!