需求每三秒定时查询,表里面才4万条记录,效率就跟不上了,如何优化?
表AlarmLog字段有tagname、timestmp、transtype,主要是要先按变量名分组,然后从每组中寻找最新的一条记录。
select a.tagname,b.transtype,a.times from
(select tagname,max(timestmp) as times from alarmlog group by tagname) a,
(select tagname,transtype,timestmp from alarmlog) b
where a.tagname = b.tagname and a.times = b.timestmp
------解决方案--------------------
没有其他where条件可用?另外贴执行计划看看,ctrl+m 然后执行你的语句
------解决方案--------------------
你的代码应该等价于这个吧。。
SELECT
tagname,transtype,MAX(timestmp) AS times
FROM
alarmlog
GROUP BY
tagname,transtype
------解决方案--------------------
SELECT
tagname,transtype,MAX(timestmp) over(partition by tagname,transtype) AS times
FROM
alarmlog
和这个的结果呢?
------解决方案--------------------
SELECT
tagname,transtype,MAX(timestmp) over(partition by tagname) AS times
FROM
alarmlog
where
transtype='InAlm'
如果这个数据是对的话,在tagname,transtype做一个聚集索引。然后timestmp做个非聚集索引试试