mysql存储过程的作用,在SQL和MySQL中创建存储过程有什么好处?

I have a theoretical question.

I can't see any difference between declaring a function within a PHP file and creating a stored procedure in a database that does the same thing.

Why would I want to create a stored procedure to, for example, return a list of all the Cities for a specific Country, when I can do that with a PHP function to query the database and it will have the same result?

What are the benefits of using stored procedures in this case? Or which is better? To use functions in PHP or stored procedures within the database? And what are the differences between the two?

Thank you.

解决方案

Some benefits include:

Maintainability: you can change the logic in the procedure without needing to edit app1, app2 and app3 calls.

Security/Access Control: it's easier to worry about who can call a predefined procedure than it is to control who can access which tables or which table rows.

Performance: if your app is not situated on the same server as your DB, and what you're doing involves multiple queries, using a procedure reduces the network overhead by involving a single call to the database, rather than as many calls as there are queries.

Performance (2): a procedure's query plan is typically cached, allowing you to reuse it again and again without needing to re-prepare it.

(In the case of your particular example, the benefits are admittedly nil.)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值