如何在MYSQL中创建索引?

如何在MYSQL中创建索引?,mysql,sql,mysql-workbench,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

我有这样一个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不支持“包含”,所以您必须创建多列索引

请在此处阅读更多信息: 试试这个:

CREATE INDEX IDX_transactiondetails_ID_Branch ON  transactiondetails (tx_voucher_id, tx_voucher_branch, accounthead_id, narration, CREDITDEBIT, amount)
MySQL不支持“包含”,所以您必须创建多列索引

请在此处阅读更多信息:

mysql不支持
INCLUDE
像在
SQL Server
中一样。您必须包含所有列名<代码>在transactiondetails上创建索引IDX\U transactiondetails\U ID\U Branch(tx\U凭证ID、tx\U凭证\U Branch、账户头\U ID、旁白、贷记借记、金额)提示:ANSI SQL标准中未提及索引!每个品牌的SQL数据库都以自己的方式实现索引,作为特定于供应商的SQL扩展。因此,您确实需要了解所使用的数据库产品支持的语法和功能,并且不要假设在Microsoft中使用的语法在任何其他品牌中都适用。mysql不支持
INCLUDE
,就像在
SQL Server
中一样。您必须包含所有列名<代码>在transactiondetails上创建索引IDX\U transactiondetails\U ID\U Branch(tx\U凭证ID、tx\U凭证\U Branch、账户头\U ID、旁白、贷记借记、金额)提示:ANSI SQL标准中未提及索引!每个品牌的SQL数据库都以自己的方式实现索引,作为特定于供应商的SQL扩展。因此,您确实需要阅读所使用的数据库产品支持的语法和功能,不要假设在Microsoft中使用的语法在任何其他品牌中都适用。感谢您提供此代码片段,它可能会提供一些有限的即时帮助。A通过展示为什么这是一个很好的解决问题的方法,并将使它对未来有其他类似问题的读者更有用。请在你的答案中添加一些解释,包括你所做的假设。这种在MySQL中创建索引的方法的美妙之处在于它也与H2兼容。谢谢我的嵌入式数据库测试又成功了!感谢您提供这段代码片段,它可能会提供一些有限的、即时的帮助。A通过展示为什么这是一个很好的解决问题的方法,并将使它对未来有其他类似问题的读者更有用。请在你的答案中添加一些解释,包括你所做的假设。这种在MySQL中创建索引的方法的美妙之处在于它也与H2兼容。谢谢我的嵌入式数据库测试又成功了!