围绕变量的行情和大括号。哪一个是最快的Mysql查询?

时间:2021-09-04 01:09:33

We know that single quotes in PHP is faster than double quotes so $foo = 'lorem ipsum'; is faster than $foo = "lorem ipsum";.

我们知道PHP中的单引号比双引号快,所以$ foo ='lorem ipsum';比$ foo =“lorem ipsum”更快;

But what about in Mysql query? Does single quotes or double quotes affect the execution speed?

但是在Mysql查询中呢?单引号或双引号会影响执行速度吗?

Consider the following different syntaxes. Which one is the fastest one? Or is there yet another syntax which is even faster?

请考虑以下不同的语法。哪一个是最快的?或者还有另一种语法甚至更快?

mysqli_query($conn, 'SELECT * FROM `mytable` WHERE `full_name` = "' . $full_name. '"');

mysqli_query($conn, "SELECT * FROM `mytable` WHERE `full_name` = '" . $full_name. "'");

mysqli_query($conn, "SELECT * FROM `mytable` WHERE `full_name` = '$full_name'");

mysqli_query($conn, "SELECT * FROM `mytable` WHERE `full_name` = '{$full_name}'");


== Edit ==
I understand that the structure of database and other factors can also affect the execution speed. But let's assume that we already have a well-structured database and exclude other factors.

==编辑==我明白数据库的结构和其他因素也会影响执行速度。但我们假设我们已经有一个结构良好的数据库并排除其他因素。

2 个解决方案

#1


2  

You are asking about MySQL when MySQL is not involved.

当MySQL不参与时,你问的是MySQL。

mysqli_query() takes two parameters, a connection to the DB and a query to execute.

mysqli_query()接受两个参数,一个与DB的连接和一个要执行的查询。

The query is a simple string, that you can build however you want.

查询是一个简单的字符串,您可以根据需要进行构建。

What you really asking is "what is the fastest way to construct my query string", which is explained in Speed difference in using inline strings vs concatenation in php5? (short answer: concatenation using single quotes).

你真正问的是“构建我的查询字符串的最快方法是什么”,这在使用内联字符串与php5中的连接的速度差异中进行了解释? (简答:使用单引号连接)。

Another way to construct your query is using prepared statements, which will be slower for a single query (you need to call two functions), but a lot faster when repeating the same query with different parameters.

构建查询的另一种方法是使用预处理语句,对于单个查询来说速度较慢(需要调用两个函数),但在使用不同参数重复相同查询时要快得多。

Regarding the use of quotes within MySQL itself:

关于在MySQL本身中使用引号:

SELECT * FROM aTable WHERE a = 'blah';

vs

VS

SELECT * FROM aTable WHERE a = "blah";

It also has been answered in SO previously, take a look at When to use single quotes, double quotes, and backticks in MySQL (short answer regarding single/double, makes no difference, single quotes is the standard)

它之前也有过回答,看看在MySQL中使用单引号,双引号和反引号(关于单/双的简答,没有区别,单引号是标准)

#2


1  

SQL Query performance is not dependent on quotation. Double quotes generally is not used in SQL, But quotes can vary from database to database.

SQL查询性能不依赖于引用。双引号通常不在SQL中使用,但引号可能因数据库而异。

SQL Query performance depends on various cases -

SQL查询性能取决于各种情况 -

Example :

示例:

  • Table size
  • 表大小
  • Joins
  • 加盟
  • Indexing
  • 索引
  • Aggregations etc
  • 聚合等

Note : But for huge number of rows, Using double quotation (") takes milliseconds differences.

注意:但对于大量行,使用双引号(“)需要几毫秒的差异。

Execute your Query String to phpMyadmin like -

执行您的查询字符串到phpMyadmin喜欢 -

SELECT * FROM YourTable WHERE title ='SOME TITLE';
SELECT * FROM YourTable WHERE title ="SOME TITLE"; #This Query will take some Milliseconds difference for huge number of rows in Different Database

#1


2  

You are asking about MySQL when MySQL is not involved.

当MySQL不参与时,你问的是MySQL。

mysqli_query() takes two parameters, a connection to the DB and a query to execute.

mysqli_query()接受两个参数,一个与DB的连接和一个要执行的查询。

The query is a simple string, that you can build however you want.

查询是一个简单的字符串,您可以根据需要进行构建。

What you really asking is "what is the fastest way to construct my query string", which is explained in Speed difference in using inline strings vs concatenation in php5? (short answer: concatenation using single quotes).

你真正问的是“构建我的查询字符串的最快方法是什么”,这在使用内联字符串与php5中的连接的速度差异中进行了解释? (简答:使用单引号连接)。

Another way to construct your query is using prepared statements, which will be slower for a single query (you need to call two functions), but a lot faster when repeating the same query with different parameters.

构建查询的另一种方法是使用预处理语句,对于单个查询来说速度较慢(需要调用两个函数),但在使用不同参数重复相同查询时要快得多。

Regarding the use of quotes within MySQL itself:

关于在MySQL本身中使用引号:

SELECT * FROM aTable WHERE a = 'blah';

vs

VS

SELECT * FROM aTable WHERE a = "blah";

It also has been answered in SO previously, take a look at When to use single quotes, double quotes, and backticks in MySQL (short answer regarding single/double, makes no difference, single quotes is the standard)

它之前也有过回答,看看在MySQL中使用单引号,双引号和反引号(关于单/双的简答,没有区别,单引号是标准)

#2


1  

SQL Query performance is not dependent on quotation. Double quotes generally is not used in SQL, But quotes can vary from database to database.

SQL查询性能不依赖于引用。双引号通常不在SQL中使用,但引号可能因数据库而异。

SQL Query performance depends on various cases -

SQL查询性能取决于各种情况 -

Example :

示例:

  • Table size
  • 表大小
  • Joins
  • 加盟
  • Indexing
  • 索引
  • Aggregations etc
  • 聚合等

Note : But for huge number of rows, Using double quotation (") takes milliseconds differences.

注意:但对于大量行,使用双引号(“)需要几毫秒的差异。

Execute your Query String to phpMyadmin like -

执行您的查询字符串到phpMyadmin喜欢 -

SELECT * FROM YourTable WHERE title ='SOME TITLE';
SELECT * FROM YourTable WHERE title ="SOME TITLE"; #This Query will take some Milliseconds difference for huge number of rows in Different Database