PHP的in_array与MySQL的SELECT [英] PHP's in_array vs. MySQL SELECT

查看:121
本文介绍了PHP的in_array与MySQL的SELECT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查,如果某个整数值已经是我的数据库中(这是越来越所有的时间)。它应该在一个脚本来完成数千次。我正在考虑两种选择:

I need to check if some integer value is already in my database (which is growing all the time). And it should be done several thousand times in one script. I'm considering two alternatives:


  1. 阅读从MySQL数据库中的所有这些数字到PHP数组,每次我需要检查它,使用in_array功能的时间。

  2. 每次我需要检查的数量,只需要执行类似SELECT数量从表,其中数='#'LIMIT 1

在一方面,其中存储在RAM中应该不是每次查询mysql的更快阵列搜索(正如我刚才所说,这些检查了一千倍一个脚本,执行过程中执行)。在另一方面,DB不断增长,蚂蚁阵列可能会变得非常大,可能会慢下来。

On the one hand, searching in array which is stored in RAM should be faster than querying mysql every time (as I have mentioned, these checks are performed about a thousand times during one script execution). On the other hand, DB is growing, ant that array may become quite big and that may slow things down.

问题是 - 它的方法是通过一些其他方面的更快,更好。

Question is - which way is faster or better by some other aspects?

推荐答案

我不得不承认,#2是您最佳的选择。当执行与 LIMIT 1查询停止MySQL的查询时找到的第一个匹配。确保列你打算通过进行索引搜索。

I have to agree that #2 is your best choice. When performing a query with a LIMIT 1 MySQL stops the query when it finds the first match. Make sure the columns you intend to search by are indexed.

这篇关于PHP的in_array与MySQL的SELECT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆