To optimize the given SQL query, you can follow the following steps:

  1. Remove the unnecessary backticks around the column aliases (key, triggerId).
  2. Remove the unnecessary comment (-- h.host) from the query.
  3. Use INNER JOIN instead of LEFT JOIN for the table "hosts" since the condition "f.itemid is not null" already filters out the rows where there is no matching host.
  4. Add appropriate indexes on the columns being used in JOIN conditions and WHERE clause.

The optimized SQL query would be:

SELECT DISTINCT i.key_, i.description, f.triggerid AS triggerId
FROM items i
INNER JOIN hosts h ON i.hostid = h.hostid
LEFT JOIN functions f ON i.itemid = f.itemid
WHERE f.itemid IS NOT NULL AND f.name <> 'nodata';

Make sure to create indexes on the columns "items.hostid", "hosts.hostid", "functions.itemid", and "functions.name" for better performance

sql 优化select distinct ikey_ key idescription ftriggerid triggerId -- hhostfrom items ileft join hosts h on ihostid = hhostidleft join functions f on iitemid = fitemidwhere fitemid is not null and fnam

原文地址: https://www.cveoy.top/t/topic/hE9J 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录