简体   繁体   English

WHERE 子句对性能的影响

[英]Impact of WHERE clause on performance

I am no SQL expert and I am trying to optimize a query I'm making.我不是 SQL 专家,我正在尝试优化我正在做的查询。 Assume the following situation: I want to extract all the unpaid bills for a list of clientID.假设以下情况:我想为一个 clientID 列表提取所有未付账单。 My current query looks a bit like this:我当前的查询看起来有点像这样:

SELECT CLIENT_ID, BILL_ID, BILL_PAID  
FROM T_BILLS 
WHERE CLIENT_ID IN [LIST OF IDs] 
  AND BILL_PAID = 'No' 

My first thinking was that the second where condition (BILL_PAID = 'No') is reducing the number of rows returned thus making the query faster.我的第一个想法是第二个 where 条件 (BILL_PAID = 'No') 正在减少返回的行数,从而使查询更快。 My company server has a time limit for running SQL Queries.我公司的服务器有运行 SQL 查询的时间限制。 Right now I'm busting it.现在我正在破坏它。 I know I could slice my LIST OF IDs into smaller lists but before doing so I was wondering if eliminating the second where clause could help.我知道我可以将我的 LIST OF ID 分成更小的列表,但在这样做之前我想知道消除第二个 where 子句是否有帮助。 The logic being I would get more data but that might require less computing.逻辑是我会得到更多的数据,但这可能需要更少的计算。 The extra data could easily be filtered out later with excel.稍后可以使用 excel 轻松过滤掉额外的数据。

As mentioned in comments if you don't have indexes on CLIENT_ID and BILL_PAID then add them.如评论中所述,如果您在CLIENT_IDBILL_PAID上没有索引,请添加它们。 Also consider change BILL_PAID type from string to int or tiny int (model enum or boolean type if applicable) to bust the index on that column even more.还要考虑将BILL_PAID类型从字符串更改为 int 或 tiny int(模型枚举或 boolean 类型,如果适用)以进一步破坏该列上的索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM