热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

PHP:帮助实现一系列查询-PHP:Helpwithpreformanceofaseriesofqueries

Ihaveamethodthatloopsthroughalltherowsofatablewhereacertainconditionismet,andthe

I have a method that loops through all the rows of a table where a certain condition is met, and then checks if one of the columns in the row appears in a text. The method is below:

我有一个方法,它循环遍历表中的所有行,其中满足某个条件,然后检查行中的其中一个列是否出现在文本中。下面的方法是:

public function isRecipeType($ingredients, $type)
{
    $rows = $this->fetchAll($this->select()->where("type = ?", $type));

    foreach($rows as $row)
    {
        if((strpos($ingredients, $row->name)) !== false)
        {
            return true;
        }
    }
}

This takes forever, though. How can I speed this up (without removing rows from my table)?

不过,这需要永远。如何加快速度(不从表中删除行)?

3 个解决方案

#1


3  

The problem you are trying to solve could be expressed as "are there any rows, with type=X, whose name contains text Y?"

您试图解决的问题可以表示为“是否有任何行,类型为=X,其名称包含文本Y?”

This is a question the database can answer, without you having to write a loop in PHP. It's quicker to use the database, the database has access to more information about tuning than you do. It's inefficient to basically download the entire subset of the table matching that type to the PHP processor, from the database, probably over a network, in order to answer the query. Let the DB do the work for you. It's also easier to read as SQL is a declarative language, as opposed to the PHP solution being imperative.

这是数据库可以回答的问题,无需编写PHP循环。使用数据库更快,数据库可以访问更多关于调优的信息。从数据库(可能是通过网络)下载与该类型匹配的表的整个子集,以便回答查询,这样做效率会很低。让DB为你做这项工作。它也更容易阅读,因为SQL是一种声明性语言,而PHP解决方案是必需的。

public function isRecipeType($ingredients, $type)
{
    $sql = "SELECT COUNT(*) AS c ".
           "FROM table ".
           "WHERE type = ? ".
           "   AND ? LIKE CONCAT('%', name, '%')";
    $rows = execute($sql, $type, $ingredients);
    $row = $rows[0];
    return $row["c"] > 0;
}

This uses MySQL syntax, if you use another database then replace the last line of the SQL with

这使用了MySQL语法,如果您使用另一个数据库,那么请将SQL的最后一行替换为

           "   AND ? LIKE '%' || name || '%'";

I have invented an execute method in your code there, I don't know what database access layer you are using, but no doubt you will have access to something similar.

我在您的代码中创建了一个execute方法,我不知道您正在使用哪个数据库访问层,但是毫无疑问您将访问类似的东西。

As another answer says, make sure you have an index on the type column. The execution plan for this statement will then be: Find all the rows with the matching type (using the index) and then go through the data and apply the LIKE.

正如另一个答案所说,请确保在type列上有索引。该语句的执行计划将是:查找具有匹配类型的所有行(使用索引),然后遍历数据并应用类似的操作。

Unless more than e.g. 10% of the rows match the type column, then a sequential read over all the data (i.e. not using the index) will be faster than using the index to identify rows and then random-access reading them. But the database will know if that's the case or not, it doesn't hurt to create the index, then the database can use it or not based on this criteria.

除非有超过10%的行与类型列匹配,否则顺序读取所有数据(即不使用索引)将比使用索引来识别行和随机访问读取它们更快。但是数据库将知道,如果是这样的话,那么创建索引并没有什么害处,那么数据库可以根据这个条件使用它或不使用它。

#2


2  

First things first, do you have an index defined on the type column? With that being the only condition in your search, you'll want it to be an efficient one.

首先,您是否在type列上定义了索引?这是你搜索的唯一条件,你会希望它是一个有效的条件。

#3


2  

Can´t you do everything in SQL?

可以在SQL ?´t你所做的一切

Corrected - untested - example:

校正-未经测试-示例:

"... WHERE `type`=? AND FIND_IN_SET(name, `$ingredients`) > 0 LIMIT 1"

// return TRUE if a row is found

For FIND_IN_SET to work, the values have to be separated by commas so you might have to transform your $ingredients variable.

对于FIND_IN_SET的工作,这些值必须用逗号分隔,因此您可能需要转换$成分变量。


推荐阅读
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • ALTERTABLE通过更改、添加、除去列和约束,或者通过启用或禁用约束和触发器来更改表的定义。语法ALTERTABLEtable{[ALTERCOLUMNcolu ... [详细]
  • mysql-cluster集群sql节点高可用keepalived的故障处理过程
    本文描述了mysql-cluster集群sql节点高可用keepalived的故障处理过程,包括故障发生时间、故障描述、故障分析等内容。根据keepalived的日志分析,发现bogus VRRP packet received on eth0 !!!等错误信息,进而导致vip地址失效,使得mysql-cluster的api无法访问。针对这个问题,本文提供了相应的解决方案。 ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • 本文详细介绍了如何使用MySQL来显示SQL语句的执行时间,并通过MySQL Query Profiler获取CPU和内存使用量以及系统锁和表锁的时间。同时介绍了效能分析的三种方法:瓶颈分析、工作负载分析和基于比率的分析。 ... [详细]
  • 【Mysql】九、Mysql高级篇 索引
    MYSQL索引一、什么是索引?二、索引数据结构1、mysql数据库的四种索引2、BTREE结构三、索引分类、创建索引、查看索引1、单值索引2、复合索引3、函数索引4、 ... [详细]
  • 导读执行UPDATE时,WEHRE条件列虽已有索引,但还会锁全表,肿么回事?问题描述叶师傅有次上课过程中执行UPDATE测试案例时,发现虽然WHERE条件列已有索 ... [详细]
  • SoIhavealoopthatrunsperfectforeventsandonlyshowsfutureposts.TheissueisthatIwould ... [详细]
  • 基于PgpoolII的PostgreSQL集群安装与配置教程
    本文介绍了基于PgpoolII的PostgreSQL集群的安装与配置教程。Pgpool-II是一个位于PostgreSQL服务器和PostgreSQL数据库客户端之间的中间件,提供了连接池、复制、负载均衡、缓存、看门狗、限制链接等功能,可以用于搭建高可用的PostgreSQL集群。文章详细介绍了通过yum安装Pgpool-II的步骤,并提供了相关的官方参考地址。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • 展开全部下面的代码是创建一个立方体Thisexamplescreatesanddisplaysasimplebox.#Thefirstlineloadstheinit_disp ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • 本文介绍了Windows操作系统的版本及其特点,包括Windows 7系统的6个版本:Starter、Home Basic、Home Premium、Professional、Enterprise、Ultimate。Windows操作系统是微软公司研发的一套操作系统,具有人机操作性优异、支持的应用软件较多、对硬件支持良好等优点。Windows 7 Starter是功能最少的版本,缺乏Aero特效功能,没有64位支持,最初设计不能同时运行三个以上应用程序。 ... [详细]
  • Ihaveaworkfolderdirectory.我有一个工作文件夹目录。holderDir.glob(*)>holder[ProjectOne, ... [详细]
author-avatar
有海的地方最美_171
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有