mysql - MYSQL如何建立索引?

标签 mysql sql mysql-workbench

我有一个像这样的 sql 查询:

 Select ah.CODE,
          case when ( (t.narration)='' OR (t.narration) is NULL) then concat(isnull(v.nameandaddress,''),' ', (v.remarks)) else (t.narration) end as narration, 
          v.VOUCHERNO,  
          case when t.CREDITDEBIT = 1 then t.amount else 0 end as dr_amount,  
          case when t.CREDITDEBIT = 0 then t.amount else 0 end as cr_amount,  
          v.issueDate,
          ag.NAME , 
          ah.name, 
          (v.remarks) 
from voucher v   
inner join transactiondetails t on t.tx_voucher_id = v.voucherId and 
t.tx_voucher_branch = v.sourceUnit  
inner join accounthead ah on t.accounthead_id = ah.ID  
inner join accountgroup ag on ag.ID=ah.accountgroup_id  
 where v.sourceunit=279 
   and v.issueDate between '2017-04-01 00:00:00' and '2018-01-18 00:00:00' 
   and v.ISCANCELLED=0 
   and ah.code in ('1412')  
order by ah.name, v.issuedate, v.voucherid

如果我给表 transactiondetails 一个索引,它会帮助这个查询吗?

创建的索引如下所述

CREATE INDEX IDX_transactiondetails_ID_Branch ON  transactiondetails 
(tx_voucher_id,tx_voucher_branch,accounthead_id) INCLUDE 
 (narration,CREDITDEBIT,amount)

从上面的索引创建查询中,我们对某些列使用了 INCLUDE 关键字。现在我需要知道,如何在 MySQL 中创建上述索引创建查询

最佳答案

试试这个:

CREATE INDEX IDX_transactiondetails_ID_Branch ON  transactiondetails (tx_voucher_id, tx_voucher_branch, accounthead_id, narration, CREDITDEBIT, amount)

MySQL不支持“INCLUDE”,所以你必须创建一个多列索引。

在这里阅读更多: https://dev.mysql.com/doc/refman/5.7/en/multiple-column-indexes.html

关于mysql - MYSQL如何建立索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48437060/

相关文章:

python - MySQL 没有给出错误,但是没有一行被填充

mysql - Node.js 从一个函数返回一个 promise

mysql - SQL查询子查询结构

mysql - myisamchk : error: 140 when opening MyISAM-table

mysql - Sql LIKE 一系列数字

sql - 如何以 “12”的格式显示数字 “0000012”

mysql - SQL Server 2008 R2 到 MySQL 迁移

Java应用程序: accessing database on a different computer

mysql - 获取每个日期的最高值

Mysql 如何在查询结果中找到相同的值