我正在构建一个有 Angular 的测验应用程序,以 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) (主题 ID,ID) 因为它们是常用的过滤+排序组合?
在这种情况下使用 postgresql 而不是 mysql 有帮助吗?
复合索引中的最大字段数是多少?
最佳答案
我已经找到答案了。通过创建不同的复合索引,我能够将 Node 服务器每秒的请求增加到两倍以上。复合索引众多,精准优化频繁查询。通过在添加索引之前和之后对查询进行基准测试,我能够最大程度地优化。 谢谢大家....
关于Mysql 表优化用于过滤和防止文件排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33850778/