问题
Are there any short-circuit logic operators (specifically short-circuit AND
and short-circuit OR
) that I can use in a WHERE
clause in MySQL 5.5? If there isn't, what are the alternatives?
An abstract view at my problem along with an explanation as to why I need this can be found at this fiddle:
http://sqlfiddle.com/#!2/97fd1/3
In reality we are looking at millions of books in millions of bookstores in thousands of cities in hundreds of countries, which is why we cannot accept the overhead of receiving the unneeded information with every query we dispatch and seriously need to find a way to make the evaluation stop as soon as we have all rows that satisfy the current condition, before moving on to the next OR.
Let me know if you need more information. Thanks in advance.
As requested, here is the schema used in the fiddle:
CREATE TABLE quantitycache (
id INT AUTO_INCREMENT,
quantity INT,
book_id INT NOT NULL,
bookstore_id INT NULL,
city_id INT NULL,
country_id INT NULL,
PRIMARY KEY (id)
);
As well as some example data:
INSERT INTO quantitycache
(quantity, book_id, bookstore_id, city_id, country_id)
VALUES
(5, 1, 1, NULL, NULL),
(100, 2, 1, NULL, NULL),
(7, 1, 2, NULL, NULL),
(12, 1, NULL, 1, NULL),
(12, 1, NULL, NULL, 1),
(100, 2, NULL, 1, NULL),
(100, 2, NULL, NULL, 1),
(200, 3, NULL, 1, NULL),
(250, 3, NULL, NULL, 1);
回答1:
Keep in mind that a query does not execute imperatively. The query you wrote may run on multiple threads, and therefore a short-circuit operator in the where clause would not result in only one result.
Instead, use the LIMIT
clause to only return the first row.
SELECT * FROM quantitycache
WHERE bookstore_id = 1 OR city_id = 1 OR country_id = 1
ORDER BY bookstore_id IS NULL ASC,
city_id IS NULL ASC,
country_id IS NULL ASC
LIMIT 1;
To get the best match for all books in a result set, save the results to a temp table, find the best result, then return interesting fields.
CREATE TEMPORARY TABLE results (id int, book_id int, match_rank int);
INSERT INTO results (id, book_id, match_rank)
SELECT id, book_id,
-- this assumes that lower numbers are better
CASE WHEN Bookstore_ID is not null then 1
WHEN City_ID is not null then 2
ELSE 3 END as match_rank
FROM quantitycache
WHERE bookstore_id = 1 OR city_id = 1 OR country_id = 1;
Select *
from (
select book_id, MIN(match_rank) as best_rank
from results
group by book_id
) as r
inner join results as rid
on r.book_id = rid.book_id
and rid.match_rank = r.best_rank
inner join quantitycache as q on q.id = rid.id;
DROP TABLE results;
来源:https://stackoverflow.com/questions/16969780/short-circuit-logic-evaluation-operators