废话不多说,直接描述:
分区表T, 由10个子表组成T1 T2...........T10
子表在字段uid上建立约束和索引
语句:
select * from T where uid = 1
查询优化器会自动转换为查找相对应的字表T1下面语句是问题:
declare @uid int
set @uid int = 1
select * from T where uid = @uid
当查询的条件改为变量后,发现查询优化器会对分区表所有的子表进行扫描.实在是对sql脑残的查询优化器无语了,再想办法去找常量变量,结果sql server不支持常量变量我认为,分区表极大的影响性能,稍微条件不好,查询优化器就会对所有子表进行扫描,浪费大量的运算.

解决方案 »

  1.   

    分区表是SQL 2005起才有的.
      

  2.   

    分区视图 
    分区视图是提高查询性能的一个很好的办法 
    --看下面的示例 --示例表 
    create table tempdb.dbo.t_10( 
    id int primary key check(id between 1 and 10),name varchar(10)) create table pubs.dbo.t_20( 
    id int primary key check(id between 11 and 20),name varchar(10)) create table northwind.dbo.t_30( 
    id int primary key check(id between 21 and 30),name varchar(10)) 
    go --分区视图 
    create view v_t 
    as 
    select * from tempdb.dbo.t_10 
    union all 
    select * from pubs.dbo.t_20 
    union all 
    select * from northwind.dbo.t_30 
    go --插入数据 
    insert v_t select 1 ,'aa' 
    union  all select 2 ,'bb' 
    union  all select 11,'cc' 
    union  all select 12,'dd' 
    union  all select 21,'ee' 
    union  all select 22,'ff' --更新数据 
    update v_t set name=name+'_更新' where right(id,1)=1 --删除测试 
    delete from v_t where right(id,1)=2 --显示结果 
    select * from v_t 
    go --删除测试 
    drop table northwind.dbo.t_30,pubs.dbo.t_20,tempdb.dbo.t_10 
    drop view v_t /**//*--测试结果 id          name      
    ----------- ---------- 
    1          aa_更新 
    11          cc_更新 
    21          ee_更新 (所影响的行数为 3 行) 
    ==*/ 
      

  3.   

    终于找到一个不是很好的权宜之计,你分区视图不是只有在条件为常量
    时才会定位到子表吗?
    那我就采用字符串的形式,用exec来运行.
    declare @uid int
    set @uid int = 1DECLARE @sqlStr varchar(8000)
    SET @sqlStr = 'select * from T where uid = ' + str(@uid) 
    exec(@sqlStr)
    这样就不会多所有子表进行扫描了.
    但这种方法只有在查询条件uid为固定的一个值时起作用,当@uid是另一个表返回的
    结果时,此法根本无用.