mysql - 此查询的最佳索引是什么 - 如果条件?
问题描述
我有一个带有多个 if 条件的查询,这意味着只有在有输入或数据时才执行。
下面是查询
var sql = `SELECT
COALESCE(commerce_order.total_price_usd, 0) AS 'Total price (USD)',
commerce_order.country AS Country,
commerce_order.customer_id AS 'Customer Id',
COALESCE(commerce_order.ffm_status, "unfulfilled") AS 'Fulfillment status'
FROM commerce_order
WHERE merchant_id = ?`;
var bind = [p.data.store_id]
const fromIndex = sql.indexOf('FROM');
if (p.data.hasOwnProperty('email')){
sql += " AND commerce_order.customer_email REGEXP ? "
bind.push(p.data.email);
};
if (p.data.hasOwnProperty('startDate')){
sql += " AND DATE(commerce_order.processed_at) >= ? "
bind.push(p.data.startDate);
};
if (p.data.hasOwnProperty('endDate')){
sql += " AND DATE(commerce_order.processed_at) <= ? "
bind.push(p.data.endDate);
};
if (p.data.hasOwnProperty('paymentStatus')){
if (p.data.paymentStatus !== 'all'){
sql += " AND commerce_order.financial_status = ? "
bind.push(p.data.paymentStatus);
};
};
if (p.data.hasOwnProperty('fulfillmentStatus')){
if (p.data.fulfillmentStatus !== 'all'){
if (p.data.fulfillmentStatus === 'fulfilled'){
sql += " AND commerce_order.ffm_status = ? ";
bind.push(p.data.fulfillmentStatus);
} else if (p.data.fulfillmentStatus === 'unfulfilled'){
sql += " AND commerce_order.ffm_status IS NULL "
};
};
};
if (p.data.hasOwnProperty('order_number')){
sql += " AND commerce_order.order_number = ? "
bind.push(p.data.order_number);
};
sql += " ORDER BY commerce_order.processed_at "
如何为此查询创建复合索引?还是我只需要单列索引?请指教,谢谢。
解决方案
推荐阅读
- python - Sub DataFrame 提取错误(列表索引超出范围)
- c++ - 使用模板元编程构建编译时命令查找表
- azure-devops - 我可以在不签入要掌握的 yaml 的情况下开发我的管道吗?
- javascript - 调用函数将引用错误作为函数未定义
- javascript - 如何检查从 ajax 调用返回的数据内容
- react-native - React Native 和 Mysql 连接
- flutter - 当 Flutter FutureProvider 解决它的未来时导航
- asp.net - .Net 重建后始终保留旧数据
- python - 平衡两个数据集进行训练
- twitter-bootstrap - 如何将引导页脚设置到页面末尾?