问题描述
我有一个包含大量数据的表格——它只是一列包含一百万个电话号码的数据.
I have a table of a ton of data--it's just a single column of a million phone numbers.
我正在编写一个 PHP 脚本,该脚本将接收上传的数字文件,我想针对数据库运行每个数字以查看该数字是否已存在.如果确实存在,我只需要返回一个 true 而不需要其他数据.
I'm working on a PHP script that will receive an uploaded file of numbers, and I'd like to run each number against the database to see if the number already exists. If it does exist, I just need a true returned and no other data.
我有两个主要问题:
- 出于性能/速度的考虑,如果
- 该列分为两部分(A 列带有区号,B 列带有剩余数字).这样我就可以使用 WHERE 运行查询来查找给定号码的匹配区号,然后匹配剩余的 7 位数字?
- 或者这与匹配整个 10 位数字没有区别?
这个问题的答案,测试 MySQL 表中是否存在行的最佳方法,是:
The answer to this question, Best way to test if a row exists in a MySQL table, was:
SELECT EXISTS(SELECT 1 FROM table1 WHERE ...)
如果我没记错的话,如果您要搜索的内容存在,那只会返回值1",对吗?由于我将在 PHP 中接收结果,这是最有效的方法吗?
If I'm not mistaken, that will simply return the value "1" if what you're searching for exists, correct? Since I'll be receiving the results in PHP, would this be the most efficient way to go?
推荐答案
出于性能原因,我会在一个查询中选择所有数字.
For performance reasons I would select all numbers in one query.
SELECT `numbers` FROM `phone_numbers` WHERE `number` IN('123', '456', '789')
然后您可以迭代结果并检查其中是否包含数字.如果不包含数字,则将其附加到您的插入查询中.所以你只有两个查询.一个用于选择,一个用于插入.
Then you can iterate over the result and check if a number is contained in it or not. If a number isn't contained then append it to your insert query. So you have only two queries. One for select and one for insert.
关于您的性能问题.您应该在存储电话号码的字段上有一个索引.
To your performance question. You should have an index on your field which stores the phone numbers.
这篇关于检查 mySQL 中现有条目的最佳方法是什么?(简单的表结构,大量的数据)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!