SQL函数的单独查询?
我使用的是 php 和 MySql 的组合。我的情况是,我在一项活动中使用两个查询(一个“选择”和“插入”),有时使用 3 个查询。不同的用户会频繁地执行此操作。
我单独使用 mysql_query 函数来执行查询。这是好还是使用执行所有查询的 SQL 函数更好?我想知道哪个在性能方面更好。
I am using a combination of php and 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. Is this good or is it better to use a SQL function which executes all the queries. I want to know which is better regarding performance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在我看来,更好的做法是创建一个能够很好地执行单个操作的函数。这使得这些功能更容易测试,并允许它们在未来的其他操作中使用。在您的情况下,我将创建一个 存储过程 来执行例行公事。
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.
如果您使用
mysqli
扩展,您可以使用multi_query
:http://php.net/manual/en/mysqli.multi-query.php
但是请注意,如果其中一个查询依赖于另一个查询的成功,应该将它们分开以进行错误检查。
If you're using the
mysqli
extension you can utilizemulti_query
: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.
如果你能展示具体的例子将会有所帮助——现在,这一切都非常假设。但是:
如果您可以将这些操作合并到一个查询中,那么它是最快的选择。例如:
如果您需要对select语句的结果进行一些处理,您可以创建一个存储过程来进行处理。这避免了将数据发送回 PHP 服务器的往返过程,这应该会带来性能优势 - 但是,PHP 很可能是比存储过程更好/更快执行处理的语言。例如,如果您必须操作文本,我会用 PHP 来完成。
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:
If you have to do some processing on the results of the select statement, you can create a stored procedure to do the processing. 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. For instance, if you have to manipulate text, I'd do it in PHP.