优化多个OR查询

发布于 2024-10-13 08:06:25 字数 688 浏览 0 评论 0原文

我有一个数据库表,我需要验证用户是否输入了相同或部分相同的信息。

这就是我的想法

数据库布局

rec_id (pk), user_id,
name, phone, address_1, address_2, zip, 
company, co_phone, co_address_1, co_address_2, co_zip, 
billing, bi_phone, bi_address_1, bi_address_2, bi_zip

查询

SELECT rec_id 
FROM tbl_name
WHERE user_id = '123456789'
OR '1112223333' IN (phone, co_phone, bi_phone)
OR 'John Doe' IN (name, business, billing)
OR '12345' IN (zip, co_zip, bi_zip)
OR '123 main street' IN (address_1, co_address_1, bi_address_1)
OR 'po box 123' IN (address_2, co_address_2, bi_address_2)

如果任何数据匹配(是的,这将是误报)我需要旧的rec_id。

想知道是否有更好的方法来做到这一点?

谢谢

I have a Database table where I need to validate if a user has entered in the same or partly the same information.

Here is what I'm thinking

The db layout

rec_id (pk), user_id,
name, phone, address_1, address_2, zip, 
company, co_phone, co_address_1, co_address_2, co_zip, 
billing, bi_phone, bi_address_1, bi_address_2, bi_zip

The Query

SELECT rec_id 
FROM tbl_name
WHERE user_id = '123456789'
OR '1112223333' IN (phone, co_phone, bi_phone)
OR 'John Doe' IN (name, business, billing)
OR '12345' IN (zip, co_zip, bi_zip)
OR '123 main street' IN (address_1, co_address_1, bi_address_1)
OR 'po box 123' IN (address_2, co_address_2, bi_address_2)

If any of the data matches (and yes the will be false positives) I need the old rec_id.

Wanted to know if there is a better way to do this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何必那么矫情 2024-10-20 08:06:25

为了使该查询性能良好,您需要为正在测试的每个列建立单独的索引。所有列上的组合索引对于 OR 条件根本没有帮助(但如果有 AND 则会有所帮助)。

但是,我认为无论您添加哪些索引,您的查询都会导致全表扫描。您可能想尝试使用 UNION 而不是 OR 来看看这是否有什么不同:

SELECT rec_id FROM tbl_name WHERE tax_id = '123456789'
UNION
SELECT rec_id FROM tbl_name WHERE phone = '1112223333'
UNION
SELECT rec_id FROM tbl_name WHERE co_phone = '1112223333'
UNION
SELECT rec_id FROM tbl_name WHERE bi_phone = '1112223333'
UNION
SELECT rec_id FROM tbl_name WHERE name = 'John Doe'
UNION
SELECT rec_id FROM tbl_name WHERE business = 'John Doe'
UNION
SELECT rec_id FROM tbl_name WHERE billing = 'John Doe'
UNION
-- etc...

像这样重写它的想法是现在每个子查询将能够使用索引(当然假设您已经添加了必要的索引)。

For this query to perform well you will need separate indexes for each of the columns you are testing. A combined index on all columns won't help at all for OR conditions (it would help if you had ANDs though).

However I would imagine that your query result in a full table scan regardless of which indexes you add. You might want to try using UNIONs instead of OR to see if that makes a difference:

SELECT rec_id FROM tbl_name WHERE tax_id = '123456789'
UNION
SELECT rec_id FROM tbl_name WHERE phone = '1112223333'
UNION
SELECT rec_id FROM tbl_name WHERE co_phone = '1112223333'
UNION
SELECT rec_id FROM tbl_name WHERE bi_phone = '1112223333'
UNION
SELECT rec_id FROM tbl_name WHERE name = 'John Doe'
UNION
SELECT rec_id FROM tbl_name WHERE business = 'John Doe'
UNION
SELECT rec_id FROM tbl_name WHERE billing = 'John Doe'
UNION
-- etc...

The idea of rewriting it like this is that now each subquery will be able to use an index (assuming of course that you have added the necessary indexes).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文