简体   繁体   中英

Does Adding More Filters to SQL Queries Slow it Down?

I have this query:

 $query = $this->pdo->prepare("SELECT * FROM `" . $this->table . "` WHERE type='hod' OR type='special'");

I want to add more filters such as:

$query = $this->pdo->prepare("SELECT * FROM `" . $this->table . "` WHERE type='hod' OR type='special' AND label!='space'");

By doing this, am I actually speeding up or slowing down the process? Meaning, by adding additional filters, will it slow up the load time on the page or increase load time? It seems it has to look at more of the table.

The question of whether or not adding additional filters slows down a query is too broad to answer. The answer is, it depends.

If the additional filter uses AND as the logical connector and it's a filter on an indexed field where it's checking for equivalence, then it will probably be faster.

AND fieldname = 'value'

However, if the additional filter uses OR as the logical connection and there's no index, it may very well slow it down.

OR fieldname = 'value'

The only way to know for sure is to try it and run and collect stats on the different queries to see which is faster.

If you find that an OR is causing your query to run slowly, that can often be remedied by converting it into a UNION ALL query.

The question was asked years ago, but it showed up high in my Google search results and the answer doesn't quite match my real-world experiences.

Unfortunately, it is not as simple as adding more or fewer filters. It highly depends on which strategy is used. For specific cases this can be finetuned, but for general situations (dynamic queries) this can be very tricky.

Example

I'm using an InnoDB table, called water_dispense that consists of 14.5 million rows. This table has a couple of fields, but for filtering I have a hub_id (a foreign key) and an indexed date field.

Here are the results of 3 queries:

SELECT COUNT(1) FROM water_dispense WHERE hub_id = 263

The count-result is ~857k and it took ~0.1s to run this query.

SELECT COUNT(1) FROM water_dispense WHERE `date` >= '2020-01-01' AND `date` <= '2020-12-31';

The count-result is 3.3M and it took ~0.65s to run this query.

SELECT COUNT(1) FROM water_dispense WHERE `date` >= '2020-01-01' AND `date` <= '2020-12-31' AND hub_id = 263;

The count-result is 261k, but this took 4.5s to run!

When I run EXPLAIN I find that the indices aren't used in the last query. Adding more filters can result in mysql using different strategies that can greatly affect performance.

After adding an index for the combined fields 'hub_id' and 'date' mysql picks the index-strategy and greatly improves the performance. Re-running the last query after adding the new index takes 0,06s.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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