sql - 检索订单

sql - retrieving order

昨天我向一位同事解释了我的 SQL 查询,他问了我一个我从未考虑过的问题。

假设我有一个叫 the_table 的 table 像这样

col1            col2
www.toto.com    stuff_wedding
www.toto.com    stuff_boxing
www.toto.com    stuff_love
www.toto.com    stuff_wedding

我的查询是:

SELECT * FROM the_table WHERE col1 LIKE '%toto.com%' AND col LIKE '%wedding%';

我应该得到

col1            col2
www.toto.com    stuff_wedding
www.toto.com    stuff_wedding

问题如下:

当像我的情况一样执行 AND 时,SQL 是否对每一行进行解析,在 col1 上查找 toto,一旦查询找到它们,将所有结果与 col1 中的 toto 重新组合并查看在那个特定子集的 col2 中举行婚礼?

或者它是否查看任何一行,寻找 col1 的 toto,然后如果 col1 中有 toto,则寻找 col2,然后寻找 wedding?

我对那个有点困惑,因为我从来没有真正想过它。另外,数据库引擎是否与解析的完成方式有关?

谢谢

对于 SQL 服务器检查此 Understanding-how-SQL-Server-executes-a-query

我想你的答案在这里:

Seek Operator
The seek operator can locate a row directly based on a key. Seek can only operate on B-Tree organized data sources, so it can only be applied to Clustered and Nonclustered indexes. If an index has a complex key (multiple columns) then the Seek operator can only operate if values for the leftmost keys in the index definition are provided. To give an example, if an index has the key columns (A, B, C) then the Seek can locate the first row where A='a', or the first row where A='a' AND B='b' or the first row where A='a' AND B='b' AND C='c'. However, on such an index, Seek cannot locate a row where B='b' or a row where C='c'. Seek operator is also capable of implementing ranges. Given the same index definition on (A, B, C) a Seek operator can iterate all rows where A > 'a' AND A < 'z' or all rows where A = 'a' AND B > 'b' AND B < 'z', but it cannot iterate rows where B > 'b' AND B < 'z'. If you inspect an execution plan you will possibly see any of the operators Clustered Index Seek, or Remote Index Seek. They are distinct operators because they apply to different data sources, but they all have in common the capability to efficiently locate locate a row base dona key value or to iterate efficiently over a range of key values. Obviously there are no heap seek operators, as heaps, being unordered, do not have the capability to locate a row efficiently based on a key. Seek should be be the preferred data access method in almost every situation.