Mysql 表优化用于过滤和防止文件排序

标签 mysql angularjs node.js postgresql

我正在构建一个有 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/

相关文章:

javascript - 带有 angular-ui Bootstrap 的 AngularJS - 使其与 IE8 兼容

node.js - 设计Redis结构需要建议

php - 如何通知mysql中的元素与输入相同?

mysql - 将数据从 Local Rails 应用程序推送到 Elastic Beanstalk

mysql - 语法错误,创建数据库

javascript - Angularjs 的 $http.get 在 IE11 中只执行一次

angularjs - 如何使用自己的后退按钮重定向到上一页?

javascript - 未调用 PassportJS 自定义身份验证回调

javascript - 在express中使用res.sendFile()时无法设置cookie

mysql - 对一组 SUMS 求和