select count(*) from l0306_coupon cn where cn.selldate >= '2011-09-01' and selldate<='2011-09-30' and cn.cinemano in (508)这里的 selldate 和cinemano 都有索引 查询结果 9000行 一共用了两分钟左右 ,求高人说明一下为什么这么慢
sql中 in 是 必须 用的 应为不知道会有几个 <= 和between 别什么区别。 求高手解答到底是哪里出了问题。
修改原来SQL :select count(*) from l0306_coupon cn where cn.cinemano =508 and cn.selldate >='2011-09-01' and selldate<='2011-09-30'http://wenku.baidu.com/view/20de0843a8956bec0975e32d.html参考地址
请教一下 举个例子select score from User where age between 18 and 25查询速度0.0147秒 这个的age是有索引的 如果是加个SUM函数这样select SUM(score) from User where age between 18 and 25如果是上面语句的话 大概的用几分钟。请问一下什么原因呢。我刚才就是要说明这方面的问题。
还有就是这个表的数据量如果很大,也会比较慢.可以试试建这两个栏位的联合索引
(2)exist的效率比in的效率高。
(3)最好将selldate >= '2011-09-01'修改为selldate>=to_date('2011-09-01','yyyy-MM-dd')
cinemano是数字型吗?这个条件应该完全可以用=,如果cinemano是字符型,也要用='508'才能用到索引。
string czkssql0 = @"select sum(decode(t.tracememo, '现金', t.price, 0)) tracesum
,sum(decode(t.tracememo, '银行卡', t.price, 0)) bksum
from l0406_daybook t
where t.tracedate between to_date('{1}' ,'yyyy-MM-dd')
and to_date('{2}','yyyy-MM-dd') and t.cinemano in ({0})
and t.tracememo in ('现金', '银行卡')";
czkssql0 = string.Format(czkssql0, cinemano, begdate, enddate);
DbCommand czkcmd0 = db.GetSqlStringCommand(czkssql0);
DataSet czkds0 = db.ExecuteDataSet(czkcmd0);其实就是类似于这一条语句,如果查询不求和的话很快,但是只要SUM了 就慢的不得了
string czkssql1 = @"select SUM(costprice) sumcostprice from l0306_coupon
where selldate between '{1}' and '{2}' and cinemano in ({0})";
czkssql1 = string.Format(czkssql1, cinemano, begdate, enddate);
DbCommand czkcmd1 = db.GetSqlStringCommand(czkssql1);
DataSet czkds1 = db.ExecuteDataSet(czkcmd1);其实就是类似于这一条语句,如果查询不求和的话很快,但是只要SUM了 就慢的不得了
这里的 IN 里面是参数都是(varchar) 类型但就是慢 其中 selldate 和 cinemano 都有索引 ,selldate 也是varchar。 这里只要SUM后就是一个慢......
如果是加个SUM函数这样select SUM(score) from User where age between 18 and 25如果是上面语句的话 大概的用几分钟。请问一下什么原因呢。我刚才就是要说明这方面的问题。
还是建议贴执行计划,不然谁也不知道发生了什么