我的查询 -
SELECT * FROM alerts a
INNER JOIN alerts_account aa ON a.alert_id=aa.alert_id
WHERE aa.account_id=638
AND a.type IN(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17)
AND CASE WHEN (type = 11 OR type = 13 OR type = 16 OR type = 17)
THEN (is_acknowledged = 0) AND (TIMESTAMPDIFF(HOUR, time, utc_timestamp()) <= 4)
ELSE 1=1
END order by a.alert_id desc limit 10
我的表格中有数百万个条目,TIMESTAMPDIFF(HOUR, time, utc_timestamp())
会减慢查询速度吗?这需要10秒以上。在这种情况下,如何改善性能。我甚至可以在这里使用CASE吗?
编辑:
警报
alert_id | type | time | details| patient_account_id
1 | 1 | 2014-10-22 05:43:45 | 2015 | 234
2 | 5 | 2014-10-22 06:21:23 | 2014 | 345
3 | 12 | 2014-10-22 14:30:23 | 2016 | 456
alert_account
alerts_account_id | alert_id |account_id| is_poped_up| is_acknowledged
1 | 1 |234 | 0 | 0
2 | 2 |345 | 1 | 1
3 | 3 |456 | 1 | 1
警报
alerts_account
答案 0 :(得分:1)
首先按照以下方式创建索引并检查效果 -
<div class="bar bar-header">
<div class="tabs-striped tabs-color-assertive">
<div class="row">
<a class="tab-item col col-sm-6" data-ink-color="#ef473a" data-ink-opacity=".35" ng-click="activeMap()" ng-class="{'active': !isActive}" > <i class="icon ion-map"></i></a>
<a class="tab-item col col-sm-6" data-ink-color="#ef473a" data-ink-opacity=".35" ng-click="activeList()" ng-class="{'active': isActive}" > <i class="icon ion-ios-list-outline"></i></a>
</div>
</div>
由于我要求获得更多性能,因此您可以进一步提供足够的性能。
即使是最精确的索引,我们只有在获得所需的计数后才能获得,但在看到您的查询之后,似乎主要是通过account_id字段过滤数据,因此您可以按照以下方式创建最终索引 -
alter table alerts_account add index idx_alert_id(alert_id);