简体   繁体   English

如何提高数百个类似的MySQL查询的效率?

[英]How can I make hundreds of similar MySQL queries more efficient?

I have an array of information about links I've found on a webpage in a PHP script. 我有一系列有关在PHP脚本的网页上找到的链接的信息。

Each link needs to be inserted into a MySQL myisam table. 每个链接都需要插入到MySQL myisam表中。

Right now, I loop through the array and run an insert query for each link. 现在,我遍历数组并为每个链接运行一个插入查询。

Is there a more appropriate way to do this so that I'm using MySQL more efficiently? 有没有更合适的方法可以做到这一点,以便更有效地使用MySQL?

You can insert several records with a single statement: 您可以使用一条语句插入几条记录:

<?php

//  Our MySQL query

$sql = "INSERT INTO beautiful (name, age)
  VALUES
  ('Helen', 24),
  ('Katrina', 21),
  ('Samia', 22),
  ('Hui Ling', 25),
  ('Yumie', 29)";

mysql_query( $sql, $conn );

?>

Source: http://www.desilva.biz/mysql/insert.html 资料来源: http : //www.desilva.biz/mysql/insert.html

You can insert multiple values in a single statement using the following syntax: 您可以使用以下语法在单个语句中插入多个值:

INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);

see Section 12.2.5 of the MySQL manual 参见MySQL手册的12.2.5节

This is especially advisable if the MySQL server is on a different host than the PHP server: in the example above, you need only a single network roundtrip rather than three. 如果MySQL服务器与PHP服务器位于不同的主机上,则这是特别可取的:在上面的示例中,您只需要一次网络往返,而不是三个。

If network latency is not an issue (if the MySQL server runs on the same server as PHP), use prepared statements like this: 如果网络延迟不是问题(如果MySQL服务器与PHP在同一台服务器上运行),请使用如下准备的语句:

$dbh = new PDO("mysql:host=127.0.0.1;port=3306;dbname=test", "root", "");

$sth = $dbh->prepare("INSERT INTO links(title, href) VALUES(:title, :href);");

while ($link = getNextLink()) {
    $sth->execute( array( "title" => $link->title, "href" => $link->href ) );
}

Prepared statements are slightly faster when executed repeatedly, because the MySQL server has to parse the SQL and run the query planner/optimizer only once. 重复执行时,准备好的语句会稍快一些,因为MySQL服务器必须解析SQL并仅运行一次查询计划程序/优化程序。

If the table has indices, you can disable them before inserting and enable them again after inserting, this makes inserting rows faster because the indices don't have to be updated after every insert (only possible for MyISAM, see section 12.1.7 of the Mysql manual ): 如果表中有索引,则可以在插入之前将其禁用,并在插入后再次启用它们,这将加快插入行的速度,因为不必在每次插入后都更新索引(仅适用于MyISAM,请参见第12.1.7节)。 Mysql手册 ):

ALTER TABLE links DISABLE KEYS
-- insert rows
ALTER TABLE links ENABLE KEYS

A while or a for each loop is sufficient enough I would think for this, seeing as the information is going to be large and I imagine dynamic. 对于每个循环,一会儿或一会儿就足够了,我会考虑到这一点,因为信息会很大,而且我想它是动态的。

You can use one query but if you have hundreds or thousands of pieces of information, or if you are going to be constantly updating the database with new data, then an array with a for each or while loop is best for this. 您可以使用一个查询,但是如果您有成百上千的信息,或者如果要不断用新数据更新数据库,则最好使用每个循环或while循环的数组。 If its just a one off with only a few pieces of information then just write out the QUERY yourself as follows 如果仅需几条信息就可以解决问题,请按照以下步骤自行写出QUERY

mysql_query("INSERT INTO table (column1, column2) VALUES ('VALUE','VALUE'),('VALUE','VALUE')"); mysql_query(“ INSERT INTO table(column1,column2)VALUES('VALUE','VALUE'),('VALUE','VALUE')”);

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

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