You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CRM自动生成的SELECT COUNT查询性能优化咨询

CRM自动生成慢查询的性能优化方案

原始慢查询SQL

SELECT 
COUNT(DISTINCT vtiger_crmentity.crmid) AS count 
FROM vtiger_production 
INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_production.productionid 
INNER JOIN vtiger_crmentityrel ON (vtiger_crmentityrel.relcrmid = vtiger_crmentity.crmid OR vtiger_crmentityrel.crmid = vtiger_crmentity.crmid) 
LEFT JOIN vtiger_productioncf ON vtiger_productioncf.productionid = vtiger_production.productionid 
LEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid 
LEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid 
WHERE vtiger_crmentity.deleted = 0 AND (vtiger_crmentityrel.crmid = 1913774 OR vtiger_crmentityrel.relcrmid = 1913774)

查询执行计划

idselect_typetabletypepossible_keyskeykey_lenrefrowsExtra
1SIMPLEvtiger_crmentityrelindex_mergecrmid_idx, relcrmid_idx, vtiger_crmentityrel_crmid_IDXcrmid_idx, relcrmid_idx4,429323Using union(crmid_idx,relcrmid_idx); Using where
1SIMPLEvtiger_productionindexPRIMARYvtiger_production_UN_PN479859Using where; Using index
1SIMPLEvtiger_crmentityeq_refPRIMARY, crmentity_deleted_idx, vtiger_crmentity_deleted_IDXPRIMARY4spvtwp.vtiger_production.productionid1Using where

性能分析

StageTime (seconds)
executing0.000078
Sending data520.865258
end0.000060

涉及表信息

DatabaseTableTable TypeEngineVersionRow FormatRowsAvg Row LengthData LengthMax Data LengthIndex LengthData FreeAuto IncrementCreate TimeUpdate TimeCheck TimeCollationChecksumCreate OptionsComment
defvtiger_crmentityBASE TABLEInnoDB10Compressed35233354817035264024402329652428802025-02-07 18:39:00.0002025-02-07 18:39:00.000utf8_general_cirow_format=COMPRESSED
defvtiger_crmentityrelBASE TABLEInnoDB10Compressed111799929333496324811980841943042025-02-12 19:38:43.0002025-02-12 19:38:43.000utf8_general_cirow_format=COMPRESSED
defvtiger_groupsBASE TABLEInnoDB10Compressed008192819202025-01-30 22:34:50.0002025-01-30 22:34:50.000utf8_general_cirow_format=COMPRESSED
defvtiger_productionBASE TABLEInnoDB10Compressed79859292367488238387226214402025-01-30 22:51:16.0002025-01-30 22:51:16.000utf8_general_cirow_format=COMPRESSED
defvtiger_productioncfBASE TABLEInnoDB10Compressed88526201843200026214402025-01-30 22:51:18.0002025-01-30 22:51:18.000utf8_general_cirow_format=COMPRESSED
defvtiger_usersBASE TABLEInnoDB10Compressed4159924576163840532025-01-30 22:51:46.0002025-01-30 22:51:46.000utf8_general_cirow_format=COMPRESSED

服务器配置与核心参数

  • 服务器RAM:32G
  • InnoDB总数据量:7G(共1335张表)
  • 关键配置参数:
variablevalue
innodb_buffer_pool_size17179869184
tmp_table_size1073741824
max_heap_table_size1073741824

性能优化方案(无法修改查询语句)

1. 索引结构优化

  • 给vtiger_crmentityrel创建联合索引(crmid, relcrmid),替换当前的单字段索引,避免index_merge带来的性能损耗。MySQL的index_merge在处理OR条件时效率远低于联合索引,联合索引可直接定位符合crmid=1913774 OR relcrmid=1913774的所有行。
  • 给vtiger_crmentity添加联合索引(crmid, deleted),当前查询依赖PRIMARY索引后再过滤deleted=0,联合索引可直接覆盖过滤条件,减少回表开销。
  • 检查vtiger_production的vtiger_production_UN_PN索引是否以productionid为前缀,若不是则调整为以productionid开头的索引,或直接使用PRIMARY索引(如果productionid是主键),当前非主键索引扫描会额外消耗资源。

2. MySQL配置参数调整

  • 提升innodb_buffer_pool_size:当前设置为16G,服务器有32G内存,建议调整为22G-24G(innodb_buffer_pool_size=24159191040),让更多表数据和索引缓存到内存,减少磁盘IO,直接降低"Sending data"阶段耗时。
  • 临时关闭index_merge优化:若创建联合索引后MySQL仍选择index_merge,可设置optimizer_switch='index_merge=off'(全局或会话级),强制使用联合索引,需先测试验证效果。
  • 调整事务日志刷写策略:若业务允许,设置innodb_flush_log_at_trx_commit=2,减少磁盘刷写频率提升IO性能,仅在服务器断电时存在少量数据丢失风险。

3. 表数据与结构优化

  • 清理vtiger_crmentityrel无效数据:该表有110万+行,清理冗余关联记录、已删除实体的关联行,减少索引扫描行数。
  • 重建大表索引:对vtiger_crmentity、vtiger_crmentityrel执行ALTER TABLE ... FORCE(InnoDB),重建索引并优化表空间,消除碎片提升扫描效率。
  • 反馈CRM厂商优化查询逻辑:vtiger_productioncf、vtiger_users、vtiger_groups为LEFT JOIN但未在查询中使用,后续可建议厂商移除不必要的关联,当前只能通过缓存或索引降低影响。

4. 缓存与间接查询优化

  • 启用查询缓存(MySQL 5.7及以下版本):设置query_cache_type=1和合适的query_cache_size,缓存查询结果,避免重复执行,不适用于数据频繁更新的场景。
  • 预计算结果存储:创建定时任务,用存储过程预先计算该查询结果并存储到临时表,让CRM查询临时表,需确保临时表数据与原表同步,适合低更新频率业务。

内容的提问来源于stack exchange,提问作者Юрий Глущенко

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 04:13:11