【问题标题】:Mysql Table Optimization For Filtering and preventing filesort用于过滤和防止文件排序的Mysql表优化
【发布时间】:2023-04-03 01:40:02
【问题描述】:

我正在构建一个以 nodejs mysql 作为后端的角度测验应用程序。测验问题表具有 id,title,choice_a,choice_b..choice_d,subject_id fk,topic_id fk,rand1,rand2,rand3 等字段

排序是 id asc,id desc,rand1 asc,rand1 desc,rand2 asc,rand2 desc,rand3 asc,rand3 desc 等以随机化顺序

rand 字段包含从 1 到 20 随机分配的值以进行随机测验

我的问题是下面给出的查询

    SELECT question.*,subject.title as subject_title,region.title as region_title,topic.title as topic_title FROM question LEFT JOIN subject ON
   (question.subject_id = subject.id) LEFT JOIN region ON (question.region_id = region.id) LEFT JOIN topic ON (question.topic_id = 
   topic.id) WHERE ((question.subject_id = '4')) ORDER BY rand1 DESC LIMIT 20

这里使用 rand1(随机分配)过滤结果。现在我想知道为 (subject_id,rand1) 添加组合索引是否有帮助?

如果是这样,我将不得不为 (subject_id,rand1) 添加组合索引 (subject_id,rand2) (subject_id,rand3) (subject_id,id) 对吧,因为它们是常用的过滤+排序组合?

在这种情况下使用 postgresql 代替 mysql 有帮助吗?

复合索引中的最大字段数是多少?

【问题讨论】:

  • 问题 1 和 2:您为什么不亲自尝试一下?解释选择...也会有所帮助。 3:您不可能期望任何人根据单个查询为您回答这个问题。 4:16 列。

标签: mysql angularjs node.js postgresql


【解决方案1】:

我已经找到答案了。通过创建不同的复合索引,我能够将节点服务器每秒的请求增加到两倍以上。复合索引很多,可以精确优化频繁查询。通过在添加索引之前和之后对查询进行基准测试,我能够优化最多。 谢谢大家……

【讨论】:

    猜你喜欢
    • 2013-08-14
    • 1970-01-01
    • 2019-12-29
    • 1970-01-01
    • 1970-01-01
    • 2017-09-09
    • 2020-08-14
    • 2013-05-08
    • 2012-03-17
    相关资源
    最近更新 更多