简体   繁体   English

PHP mysql查询中的数组

[英]Array in php mysql query

when i use arrays mysql query it's really slow. 当我使用数组mysql查询时,它确实很慢。 are there any tricks that makes this faster? 有什么技巧可以使速度更快吗?

eg: 例如:

  SELECT * 
    FROM posts
   WHERE type IN ('1','2','5')
ORDER BY id ASC

takes much longer then other queries. 比其他查询花费的时间更长。

Here you need to look two things First, ('1','2','5') this is string not integer it should be (1,2,5) . 在这里,您需要看两件事:首先, ('1','2','5')这是不是整数的字符串,应该是(1,2,5) Second, you can apply indexes on type field. 其次,您可以在类型字段上应用索引。

If type is integer type, then remove apostrophes: 如果typeinteger类型,则除去撇号:

WHERE type IN (1, 2, 5)

If not - change type type to integer 如果不是-将type类型更改为integer

There a number of things to speed up queries I'd consider looking up the following: 有很多方法可以加快查询速度,我会考虑查找以下内容:

Normalizing. 规范化。 Perhaps the structure of your tables isn't the most efficient? 也许表的结构不是最有效的?

Indexing. 索引。 This will improve query times if you KNOW what you want to search on. 如果您知道要搜索的内容,这将缩短查询时间。

EXPLAIN will tell you why your query is slow. EXPLAIN会告诉您为什么查询缓慢。 Based on that you can make adjustments to your query or your table structure to solve the problem. 基于此,您可以调整查询或表结构来解决问题。 In this case, use it like this: 在这种情况下,请像这样使用它:

EXPLAIN SELECT * FROM posts WHERE type IN (1,2,5) ORDER BY id ASC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM