简体   繁体   English

SQL函数的单独查询?

[英]separate queries of SQL functions?

I am using a combination of php and MySql. 我正在使用php和MySql的组合。 My case is I am using two queries (one 'select' and 'insert') for an activity, sometimes 3 queries. 我的情况是我对一个活动使用两个查询(一个“选择”和“插入”),有时使用三个查询。 This will be done very frequently by different users. 这将由不同的用户非常频繁地完成。

I am using mysql_query function separately to execute the queries. 我正在单独使用mysql_query函数执行查询。 Is this good or is it better to use a SQL function which executes all the queries. 使用SQL函数执行所有查询是好事还是更好? I want to know which is better regarding performance. 我想知道哪个在性能上更好。

In my opinion it is better practice to create a function which performs a single operation well. 在我看来,更好的做法是创建一个可以很好地执行单个操作的函数。 This makes the functions easier to test and allows them to be utilized in other operations in the future. 这使功能更易于测试,并允许将来在其他操作中使用它们。 In your case I would create a stored procedure to execute the routine. 在您的情况下,我将创建一个存储过程来执行例程。

If you're using the mysqli extension you can utilize multi_query : 如果您使用的是mysqli扩展名,则可以使用multi_query

http://php.net/manual/en/mysqli.multi-query.php http://php.net/manual/en/mysqli.multi-query.php

However please note that if one of the queries relies on the success of another of the queries, they should be separated out for error checking. 但是请注意,如果其中一个查询依赖于另一个查询的成功,则应将它们分开进行错误检查。

It would help if you could show concrete examples - right now, it's all very hypothetical. 如果您可以显示具体的示例,这将有所帮助-现在,所有这些都是非常假设的。 However: 然而:

If you can combine the operations into a single query, it's the fastest option. 如果您可以将操作合并为一个查询,则这是最快的选择。 For instance: 例如:

insert into tableA
select x, y, z
from tableB
where Foo = Bar

If you have to do some processing on the results of the select statement, you can create a stored procedure to do the processing. 如果必须对select语句的结果进行一些处理,则可以创建一个存储过程来进行处理。 This avoids the round trip of sending the data back to your PHP server, which should yield a performance benefit - however, it may well be that PHP is a better/faster language to execute the processing in than the stored procedure. 这避免了将数据发送回PHP服务器的往返行程,这应该会带来性能上的好处-但是,很可能PHP是比存储过程更好/更快的语言来执行处理。 For instance, if you have to manipulate text, I'd do it in PHP. 例如,如果您必须操作文本,则可以在PHP中进行操作。

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

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