现有表table1数据量在10万-20万,60多个字段,对table1按条件进行分页查询,where条件数量不定,包括(精确条件、模糊条件和in语句),此表的insert、update操作较多,索引已建
分页变量:
int now=2;//当前页
int pagesize=20;//每页显示条数
string where="cl5=2 and cl6 in(170,171,172) and cl7 like '%市场营销%'";//条件以下拼sql语句:
select * from (
select top " + pagesize+ " * from (
select top " + (now * pagesize) + " * from (
select cl1,cl2,cl3,cl4,cl5,cl6,cl7,cl8 FROM table1 where "+where+"
) a order by a.cl1 desc
) b order by b.cl1 asc
)c order by c.cl1 desc
现在的问题就是sql占用cup资源灰常大,请各位高手帮忙提出对sql语句或数据结构等其它有效的解决方案

解决方案 »

  1.   

    你本身拼接的时候有点问题1,第一步拼接的时候如下
    select top " + (now * pagesize) + " * from (
    select cl1,cl2,cl3,cl4,cl5,cl6,cl7,cl8 FROM table1 where "+where+"
    ) a order by a.cl1 desc
    改成
    select top "+(now * pagesize)+" cl1,cl2,cl3,cl4,cl5,cl6,cl7,cl8  from table1 a where "+where+" order by a.cl1 desc
    2,保证cl1上有 desc的索引
      

  2.   

    你们的表数据量很小,但还是要尽量利用索引
    尽量在常用的where字句上的字段建立索引或联合索引这需要你的监测和统计