What's the point of a stored procedure?

前端 未结 12 1020
逝去的感伤
逝去的感伤 2021-01-21 12:13

Are they useful for anything outside of a database administrator? If I understand them correctly it\'s merely queries that can be saved directly into MySQL, so it\'d be useless

相关标签:
12条回答
  • 2021-01-21 12:30

    Stored Procedures can do far more than just query the database. They can contain any T-SQL statement. So you could use them to perform business logic, execute queries, do backups etc.

    Many companies have a policy that all database activity is to be done via stored procedures. So, in now way would I say that a web development team would have no use for them. They might make great use of them.

    On the other hand, in our company, we're not using them much of all for our next generation manufacturing applications. We're using an ORM (Linq-To-SQL) instead, and have very little use for stored procedures, at this point. I suspect though we'll still use them somewhat, in order to avoid several trips back and forth to the server. Somethings are just more efficient if done in a stored procedure, if you're already doing work on the server anyway.

    0 讨论(0)
  • 2021-01-21 12:35

    Think of them as procedures in any regular program. A way of encapsulating a chunk of logic under a single invocation method.

    If you consider functions in programs useless, then this conversation ends here. If you think they're useful, then there's nowhere else to go either. Nothing forces you to use them, but they're available if you should choose to.

    0 讨论(0)
  • 2021-01-21 12:44

    It is easier to performance tune code in stored procs than what most ORMs create. It is easier to use stored procs when there are multiple applications that access the same database and might need to do the same things. It is far easier to refactor databases when all code is in stored procs because you can easily see where the changes need to be made. It is easier to use stored procs for things that don't normally hook up to ORMs like SSIS or reporting applications. Using stored procs you can limit access to only what the proc does and not allow access directly to the tables or views. This is critical in enforcing internal controls on financial data for instance and helps prevent fraud.

    I've written complex procs that were well over a 1000 lines long. Try getting an ORM to write that kind of SQL. Then try to get it so that it will run without timing out.

    0 讨论(0)
  • 2021-01-21 12:46

    In addition to what others have said about security, encapsulation, performance, etc. of stored procedures, I'd like to add that the usefulness of stored procedures increases with the richness of the stored procedure language.

    I don't have much experience with MySQL, but as far as I know the stored procedure language is pretty limited.

    T-SQL (in Microsoft SQL Server) is more capable, but has several shortcomings compared to full-featured programming languages. For example, it is not possible to declare a constant value in T-SQL, and until quite recently there was no exception handling, so error handling was a pain. There is no concept of a package, so all your code will be stand-alone procedures with no way to group them together except for a good naming convention. (Although it's true that you can write stored procedures in .NET languages.)

    On the other hand, PL/SQL (in Oracle), is a full-featured programming language with complex data types, exception handling, packages for grouping procedures (with separate public and private sections), object types, and lots and lots of built-in packages that deal with everything from file access to compression and generating web pages. All that, plus seamless integration with the database and the SQL language. Entire applications can be built using PL/SQL, without "leaving the database", so to speak. Check out http://apex.oracle.com for an example of a massive (framework) application implemented in pure PL/SQL.

    0 讨论(0)
  • 2021-01-21 12:48

    If I understand them correctly it's merely queries that can be saved directly into MySQL, so it'd be useless for any web development team to use them.

    Even with that (limited) definition, you're implying that web development teams never have any need of querying a database? Really?

    A well written set of stored procs can completely remove queries from your client application and replace all of that with calls to the procedures. Now, I'm not saying that that's the only way of doing things, or even the right way. There's quite the discussion about that going on overall. But it is a very valid way of using them.

    So, useless for a webdev team?

    0 讨论(0)
  • 2021-01-21 12:49

    Stored procedures are code that runs on the database server.

    They have a number of uses. Think: If I could run code directly on the database server, what could I use that for?

    Among their many uses, stored procedures can be used to shift some of the processing load to the database server, to reduce network traffic, and to improve security.

    http://en.wikipedia.org/wiki/Stored_procedure

    0 讨论(0)
提交回复
热议问题