[英]Indexing email,phone column in a table in mysql
I have a table with above 5 million users with an index for three columns now. 我现在有一个表格,其中有500万以上的用户,其中索引为三列。
I am updating specific column using email or phone, when am doing this sometimes i have to update 100k users, so to check with email or phone is taking a while to check first then get id and update based on id. 我正在使用电子邮件或电话更新特定的列,这样做时有时我必须更新100k用户,因此使用电子邮件或电话进行检查需要花费一些时间来首先检查然后获取ID并根据 ID进行更新。
If I make email and phone indexed (both should have duplicated values), will my query execute better than now? 如果我将电子邮件和电话编入索引(两个值都应重复),我的查询是否会比现在执行得更好? Is it best to index email and phone as both saved as Strings in Database. 最好将同时存储为字符串的电子邮件和电话编入索引。
CREATE TABLE foo (
...
PRIMARY KEY(id),
INDEX(email),
INDEX(phone)
)
If that hint does not suffice, please provide your current CREATE TABLE
plus your attempts at SQL for the 'check' and 'update'. 如果该提示不足够,请提供您当前的CREATE TABLE
以及您尝试使用SQL进行“检查”和“更新”的尝试。
If the question is about testing for two columns, here are some tips: 如果问题是关于测试两列,则有一些技巧:
SELECT ...
WHERE email = '...'
OR phone = '...'
will be slow. 会很慢。 In general OR
does not optimize well. 通常, OR
不能很好地优化。
This would be faster: 这样会更快:
( SELECT id
WHERE email = '...' )
UNION DISTINCT
( SELECT id
WHERE phone = '...' )
INSERT ... ON DUPLICATE KEY UPDATE ...
will not work because email
and phone
are not unique. INSERT ... ON DUPLICATE KEY UPDATE ...
将不起作用,因为email
和phone
不是唯一的。
What is the business logic when there are two rows with a matching email? 当两行具有匹配的电子邮件时,业务逻辑是什么? Do you update both? 两者都更新吗? What about a email match, but phone mismatch? 电子邮件匹配但电话不匹配怎么办?
If you need both to match, then this pair is optimal: 如果您需要两者都匹配,那么这对是最佳选择:
UNIQUE(email, phone)
INSERT ...
ON DUPLICATE KEY UPDATE ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.