简体   繁体   English

什么是最好使用php查询集或mysql函数?

[英]what is better to use php query set or mysql function?

If you had data in table 1 that you need to use to return data in table 2 for each row returned in table 1. What is more efficient to use a set of querys in PHP one inbedded in the while loop of the other or an SQL function within a query? 如果表1中有数据,则需要使用该数据为表1中返回的每一行返回表2中的数据。在PHP中使用一组查询(其中一个查询包含在另一个的while循环中或使用SQL)会更有效查询内的功能?

for example: 例如:

$qry = mysql_query(SELECT date FROM table1)

while($res = mysql_fetch_array($qry))
{
  $qry = mysql_query("SELECT name FROM table2 WHERE date=$res['date']")
}

or to do this as a function that returns the Id from table1 within the query. 或将此操作作为从查询中的table1返回ID的函数来执行。

A (LEFT / RIGHT) JOIN? (向左/向右)加入?

Unless I've misunderstood the question... 除非我误解了这个问题...

I think you're looking for JOIN sql syntax. 我认为您正在寻找JOIN sql语法。 If you have 2 tables: messages and author and you want to return messages with authors. 如果您有2个表:消息和作者,并且您想返回包含作者的消息。 Then you can write following SQL statement: 然后,您可以编写以下SQL语句:

SELECT m.body, a.name FROM message m 
LEFT JOIN author a ON (a.id=m.author_id)

This will return message body with corresponding author name 这将返回带有相应作者姓名的消息正文

Table author: 表作者:

  • id - primary key id-主键
  • name - name of the author 名称-作者姓名

Table message: 表消息:

  • body - text of the message 正文-消息文本
  • author_id - id of the author author_id-作者的ID

UPD1: UPD1:

This will be faster then looping each message and select an author. 这样可以更快地循环每个消息并选择作者。 Because JOIN allows you to return all data in single query (not N x queries when using for loop). 因为JOIN允许您在单个查询中返回所有数据(使用for循环时不返回N x个查询)。

UPD2: UPD2:

With your tables the query will look like: 使用表查询将如下所示:

SELECT t1.date, t2.name FROM table1 t1 LEFT JOIN table2 t2 ON (t2.date=t1.date)

It depends on if the data is easier to find during the while loop or in the query itself. 这取决于在while循环中还是在查询本身中更容易找到数据。

So if the DB has to base the sub-query on the result of each row in the main query, and there are 1000 total rows and 100 results in the main query, it has to check all of the rows 100 times, so that's 100,000 sub-queries it runs. 因此,如果数据库必须将子查询基于主查询中每一行的结果,并且主查询中总共有1000行,并且有100个结果,则它必须检查所有行100次,因此为100,000它运行的子查询。

So think it terms of the number of results of the main query. 因此认为它是主要查询结果的数量。 If your while loop has to query the DB 100 times while the DB can do it much faster and efficiently in one query, that's better. 如果while循环必须查询数据库100次,而DB在一次查询中可以更快,更高效地完成查询,那更好。 But if you want a subset of answers that you can say 'query only based on the last set of results' the while loop is better. 但是,如果您想要答案的子集,可以说“仅基于最后一组结果进行查询”,则while循环会更好。

What is more efficient to use 有什么更有效的使用

a set of querys in PHP one inbedded in the while loop of the other PHP中的一组查询,一个查询嵌入另一个的while循环中

or 要么

an SQL function within a query 查询中的SQL函数

Seems you answered your question yourself, didn't you? 看来您自己回答了问题,不是吗?

Every query you send to the dbms has to be sent over the network, parsed, analyzed then executed. 您发送到dbms的每个查询都必须通过网络发送,解析,分析然后执行。 You may want to minimize the number of queries sent to the db. 您可能希望最大程度地减少发送到数据库的查询数量。

There may be exceptions, for example if the processing of the data requires operations which the dbms is not capable of. 可能会有例外,例如,如果数据处理需要dbms无法执行的操作。

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

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