MySQL Stored Procedure Programming
Michael J. Ross writes "MySQL may be the most popular open source relational database management system (RDBMS) in the world, but during the first decade of its existence, it lacked support for stored programs, i.e., store procedures, functions, and triggers. The major commercial RDBMS vendors — including Oracle, IBM, and Microsoft — could point to this deficiency as reason enough to choose their proprietary systems over MySQL or any other open source system, such as PostgreSQL. But with the release of MySQL version 5.0, in October 2005, the "little database engine that could" dramatically improved its position against the competition. The most comprehensive discussion of these new capabilities is in the book MySQL Stored Procedure Programming." Read below for the rest of Michael's review
MySQL Stored Procedure Programming
author
Guy Harrison and Steven Feuerstein
pages
636
publisher
O'Reilly Media
rating
9
reviewer
Michael J. Ross
ISBN
0596100892
summary
A comprehensive guide to developing MySQL stored procedures, functions, and triggers.
Written by Guy Harrison and Steven Feuerstein, and published by O'Reilly Media in March 2006 under the ISBNs 0596100892 and 978-0596100896, this book is the first one to offer database programmers a full discussion of the syntax, usage, and optimization of MySQL stored procedures, stored functions, and triggers — which the authors wisely refer to collectively as "stored programs," to simplify the manuscript. Even a year after the introduction of these new capabilities in MySQL, they have received remarkably little coverage by book publishers. Admittedly, there are three such chapters in MySQL Administrator's Guide and Language Reference (2nd Edition), written by some of the developers of MySQL, and published by MySQL Press. Yet this latter book — even though published a month after O'Reilly's — devotes fewer than 50 pages to stored programs, and the material is not in the printed book itself, but in the "MySQL Language Reference" part, on the accompanying CD. That material, in conjunction with the online reference documentation, may be sufficient for the more simple stored program development needs. But for any MySQL developer who wishes to understand in-depth how to make the most of this new functionality in version 5.0, they will likely need a much more substantial treatment — and that's exactly what Harrison and Feuerstein have created.
The authors are generous in both the technical information and development advice that they offer. The book's material spans 636 pages, organized into 23 chapters, grouped into four parts, followed by an index. The first part, "Stored Programming Fundamentals," provides an introduction and then a tutorial, both taking a broad view of MySQL stored programs. The remaining four chapters cover language fundamentals; blocks, conditional statements, and iterative programming; SQL; and error handling. The book's second part, "Stored Program Construction," may be considered the heart of the book, because its five chapters present the details of creating stored programs in general, using transaction management, using MySQL's built-in functions, and creating one's own stored functions, as well as triggers. The third part, "Using MySQL Stored Programs and Applications," explains some of the advantages and disadvantages of stored programs, and then illustrates how to call those stored programs from source code written in any one of five different programming languages: PHP, Java, Perl, Python, and Microsoft.NET. In the fourth and final part, "Optimizing Stored Programs," the authors focus on the security and tuning of stored programs, tuning SQL, optimizing the code, and optimizing the development process itself.
This is a substantial book, encompassing a great deal of technical as well as advisory information. Consequently, no review such as this can hope to describe or critically comment upon every section of every chapter of every part. Yet the overall quality and utility of the manuscript can be discerned simply by choosing just one of the aforesaid Web programming languages, and writing some code in that language to call some MySQL stored procedures and functions, to get results from a test database — and developing all of this code while relying solely upon the book under review. Creating some simple stored procedures, and calling them from some PHP and Perl scripts, demonstrated to me that MySQL Stored Procedure Programming contains more than enough coverage of the topics to be an invaluable guide in developing the most common functionality that a programmer would need to implement.
The book appears to have very few aspects or specific sections in need of improvement. The discussion of variable scoping, in Chapter 4, is too cursory (no database pun intended). In terms of the book's sample code, I found countless cases of inconsistency of formatting — specifically, operators such as "||" and "=" being jammed up against their adjacent elements, without any whitespace to improve readability. These minor flaws could be easily remedied in the next edition. Some programming books make similar mistakes, but throughout their text, which is even worse. Fortunately, most of the code in this book is neatly formatted, and the variable and program names are generally descriptive enough.
Some of the book's material could have been left out without great loss — thereby reducing the book's size, weight, and presumably price. The two chapters on basic and advanced SQL tuning contain techniques and recommendations covered with equal skill in other MySQL books, and were not needed in this one. On the other hand, sloppy developers who churn out lamentable code might argue that the last chapter, which focuses on best programming practices, could also be excised; but those are the very individuals who need those recommendations the most.
Fortunately, the few weaknesses in the book are completely overwhelmed by its positive qualities, of which there are many. The coverage of the topics is quite extensive, but without the repetition often seen in many other technical books of this size. The explanations are written with clarity, and provide enough detail for any experienced database programmer to understand the general concepts, as well as the specific details. The sample code effectively illustrates the ideas presented in the narration. The font, layout, organization, and fold-flat binding of this book, all make it a joy to read — as is characteristic of many of O'Reilly's titles.
Moreover, any programming book that manages to lighten the load of the reader by offering a touch of humor here and there, cannot be all bad. Steven Feuerstein is the author of several well-regarded books on Oracle, and it was nice to see him poke some fun at the database heavyweight, in his choice of sample code to demonstrate the my_replace() function: my_replace( 'We love the Oracle server', 'Oracle', 'MySQL').
The prospective reader who would like to learn more about this book, can consult its Web page on O'Reilly's site. There they will find both short and full descriptions, confirmed and unconfirmed errata, a link for writing a reader review, an online table of contents and index, and a sample chapter (number 6, "Error Handling"), in PDF format. In addition, the visitor can download all of the sample code in the book (562 files) and the sample database, as a mysqldump file.
Overall, MySQL Stored Procedure Programming is adeptly written, neatly organized, and exhaustive in its coverage of the topics. It is and likely will remain the premier printed resource for Web and database developers who want to learn how to create and optimize stored procedures, functions, and triggers within MySQL.
Michael J. Ross is a Web programmer, freelance writer, and the editor of PristinePlanet.com's free newsletter. He can be reached at www.ross.ws, hosted by SiteGround.
You can purchase MySQL Stored Procedure Programming from amazon.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.
Written by Guy Harrison and Steven Feuerstein, and published by O'Reilly Media in March 2006 under the ISBNs 0596100892 and 978-0596100896, this book is the first one to offer database programmers a full discussion of the syntax, usage, and optimization of MySQL stored procedures, stored functions, and triggers — which the authors wisely refer to collectively as "stored programs," to simplify the manuscript. Even a year after the introduction of these new capabilities in MySQL, they have received remarkably little coverage by book publishers. Admittedly, there are three such chapters in MySQL Administrator's Guide and Language Reference (2nd Edition), written by some of the developers of MySQL, and published by MySQL Press. Yet this latter book — even though published a month after O'Reilly's — devotes fewer than 50 pages to stored programs, and the material is not in the printed book itself, but in the "MySQL Language Reference" part, on the accompanying CD. That material, in conjunction with the online reference documentation, may be sufficient for the more simple stored program development needs. But for any MySQL developer who wishes to understand in-depth how to make the most of this new functionality in version 5.0, they will likely need a much more substantial treatment — and that's exactly what Harrison and Feuerstein have created.
The authors are generous in both the technical information and development advice that they offer. The book's material spans 636 pages, organized into 23 chapters, grouped into four parts, followed by an index. The first part, "Stored Programming Fundamentals," provides an introduction and then a tutorial, both taking a broad view of MySQL stored programs. The remaining four chapters cover language fundamentals; blocks, conditional statements, and iterative programming; SQL; and error handling. The book's second part, "Stored Program Construction," may be considered the heart of the book, because its five chapters present the details of creating stored programs in general, using transaction management, using MySQL's built-in functions, and creating one's own stored functions, as well as triggers. The third part, "Using MySQL Stored Programs and Applications," explains some of the advantages and disadvantages of stored programs, and then illustrates how to call those stored programs from source code written in any one of five different programming languages: PHP, Java, Perl, Python, and Microsoft.NET. In the fourth and final part, "Optimizing Stored Programs," the authors focus on the security and tuning of stored programs, tuning SQL, optimizing the code, and optimizing the development process itself.
This is a substantial book, encompassing a great deal of technical as well as advisory information. Consequently, no review such as this can hope to describe or critically comment upon every section of every chapter of every part. Yet the overall quality and utility of the manuscript can be discerned simply by choosing just one of the aforesaid Web programming languages, and writing some code in that language to call some MySQL stored procedures and functions, to get results from a test database — and developing all of this code while relying solely upon the book under review. Creating some simple stored procedures, and calling them from some PHP and Perl scripts, demonstrated to me that MySQL Stored Procedure Programming contains more than enough coverage of the topics to be an invaluable guide in developing the most common functionality that a programmer would need to implement.
The book appears to have very few aspects or specific sections in need of improvement. The discussion of variable scoping, in Chapter 4, is too cursory (no database pun intended). In terms of the book's sample code, I found countless cases of inconsistency of formatting — specifically, operators such as "||" and "=" being jammed up against their adjacent elements, without any whitespace to improve readability. These minor flaws could be easily remedied in the next edition. Some programming books make similar mistakes, but throughout their text, which is even worse. Fortunately, most of the code in this book is neatly formatted, and the variable and program names are generally descriptive enough.
Some of the book's material could have been left out without great loss — thereby reducing the book's size, weight, and presumably price. The two chapters on basic and advanced SQL tuning contain techniques and recommendations covered with equal skill in other MySQL books, and were not needed in this one. On the other hand, sloppy developers who churn out lamentable code might argue that the last chapter, which focuses on best programming practices, could also be excised; but those are the very individuals who need those recommendations the most.
Fortunately, the few weaknesses in the book are completely overwhelmed by its positive qualities, of which there are many. The coverage of the topics is quite extensive, but without the repetition often seen in many other technical books of this size. The explanations are written with clarity, and provide enough detail for any experienced database programmer to understand the general concepts, as well as the specific details. The sample code effectively illustrates the ideas presented in the narration. The font, layout, organization, and fold-flat binding of this book, all make it a joy to read — as is characteristic of many of O'Reilly's titles.
Moreover, any programming book that manages to lighten the load of the reader by offering a touch of humor here and there, cannot be all bad. Steven Feuerstein is the author of several well-regarded books on Oracle, and it was nice to see him poke some fun at the database heavyweight, in his choice of sample code to demonstrate the my_replace() function: my_replace( 'We love the Oracle server', 'Oracle', 'MySQL').
The prospective reader who would like to learn more about this book, can consult its Web page on O'Reilly's site. There they will find both short and full descriptions, confirmed and unconfirmed errata, a link for writing a reader review, an online table of contents and index, and a sample chapter (number 6, "Error Handling"), in PDF format. In addition, the visitor can download all of the sample code in the book (562 files) and the sample database, as a mysqldump file.
Overall, MySQL Stored Procedure Programming is adeptly written, neatly organized, and exhaustive in its coverage of the topics. It is and likely will remain the premier printed resource for Web and database developers who want to learn how to create and optimize stored procedures, functions, and triggers within MySQL.
Michael J. Ross is a Web programmer, freelance writer, and the editor of PristinePlanet.com's free newsletter. He can be reached at www.ross.ws, hosted by SiteGround.
You can purchase MySQL Stored Procedure Programming from amazon.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.
You know what it is?
It's lack of trust.
MySQL started off making their fast little datastore that uses a little SQL syntax, and they told everyone under the sun that if MySQL didn't do it, you didn't need it, and ought to design around it.
Referential integrity, ACID compliance, enforcement of rules... every step along the way, they tell you you don't need it and you ought to push the missing logic to the next tier, until they get it, if they get it, then they're so great.
Couple that with the Project Mayo/DivX corporate structure, then place Postgresql next to the whole shebang as a superior alternative, and the arguments for giving MySQL any of your attention become increasingly small.
MySQL owes its success to the fact that it was always so simple to choke it off when you were giving it to people on $5/month hosting plans, so it became popular among the very cheap.
Period.
-1 Uncomfortable Truth
I would like to know what slashdotters think is a good level of stuff to be putting into stored procedures. I've seen organizations where every single select query is put into a stored procedure. I've also seen places that avoid it like the plague. Personally I like to keep as much business logic as possible out of the database, but I realize it can speed up things considerably, so I use it where speed is critical. Is there any hope that stored procedures will become cross platform and work on all databases, at least to the level of SQL, so that there's some kind of standard, or do they just push us towards vendor lock-in?
Anthropic principle: We see the universe the way it is because if it were different we would not be here to see it.
"The major commercial RDBMS vendors -- including Oracle, IBM, and Microsoft -- could point to this deficiency as reason enough to choose their proprietary sstems over MySQL or any other open source system, such as PostgreSQL..." They could not point this because PostgreSQL has stored procedures for decades... fully tested, stable, offering e bunch of languages for writing functions/triggers, including Perl, Python, Tcl and their own PlSQL that is very close to Oracle's variant.
Does it tell anything concrete about MySQL 5? No.
Free Software: the software by the people, of the people and for the people. Develop! Share! Enhance! Enjoy!
If not... the keystone that says that we will never need more than 640kB must be thrown away.
Home of Faramir Paint Shop Pro scripts
Invexi - a Phoenix, AZ based web design and web development company.
My understanding is that Firebird is by any measure more sophisticated than MySQL, but it lacks the "critical mass" of users that makes it attractive to people who need to be sure they can get ready support for their software. MySQL is available on just about every cheap hosting provider around, too, which means that a lot of ready-built open source Web apps target MySQL as their database of choice.
Put it to you this way: If the features of MySQL are "good enough" for the application you want -- which arguably goes for most of the Web apps out there -- why would you not choose MySQL? There are times when swimming upstream is a noble effort, but generally all it gets you is tired.
If, on the other hand, you have a specific application for which you need a relational data store and you need higher-end RDBMS features, by all means, choose Firebird. Only I think in those cases most people still choose PostgreSQL, for more or less the same reasons as mentioned above.
Breakfast served all day!
I too run several databases as warehouses, and consequently I don't have a crushing need for blinding speed or speed increases due to stored procedures. Consequently all our database can be dropped off a cliff, hardware replaced and warehouse rebuilt to usability in less than a week, with NO data loss as a result of the architecture. Stored procedures would make that much more difficult, unless for some reason I completely don't understand anything about data warehousing... it could happen
Support NYCountryLawyer RIAA vs People
Rest assured that if you were one of my developers you'd be out the door in no time. The application you're describing here cannot be anything north of trivial if you were able to just switch a connection string and all your messy inline SQL statements continued to run without any changes whatsoever (which I seriously doubt). Not everyone writes trivial applications.
There are reasons other than "fast" to using stored procedures. I've seen enough misuse of SPs, functions and triggers to fill up a book, but when done correctly they are simply superior to the alternative in just about every way.
Web2.0: I love when people Flickr my cuil and digg my boingboing until my google is reddit and I start to yahoo
SP are OK for complex tasks like building reports or complex searches. You won't be able to do it DB-independently, anyway. And the speed difference between SP and processing on client can often be measured in orders of magnitude.
I'm glad I don't work for you, then. Stored procedures are a tool, like any other, that come with their own set of pros and cons. In some situations, the ability to quickly migrate to an alternate database outweighs the benefits that stored procedures may provide. In other situations, it doesn't. The decision to use them or not should be based solely on business requirements, rather than the irrational hatred you seem to be using as a guide.
"The invisible and the non-existent look very much alike." -- Delos B. McKown
And you'd get sued shortly thereafter for unfair dismissal
There are plenty of good reasons to use stored procedures in a database - they simply wouldn't exist in every serious database if they were not useful. Speed isn't the only reason to use a stored procedure, they can also make some inter-application integration much easier and allow someone in control of the data model to abstract the data collection routines from the underlying data structure - which can be very, very useful. I don't want some Java programmer thinking he knows the best way to get at data in Oracle - using a stored procedure to pass complex data structures back allows tweaking of the select statements, indexes etc... without any testing required of the app layer.
D.
The point of stored procedures is not to make access easier, or anything like that. The point from the RDBMS perspective is to keep your data coherent. Data integrity is THE MOST IMPORTANT role of a database. That's why 3rd normal form is important. That's why stored procedures and triggers and rules are important.
As a database engineer, I would *definitely* fire anyone who didn't use these tools to maintain data integrity.
Microsoft is to software what Budweiser is to beer.
In some instances, it really can speed things up. I would recommend that they not be used, except where speed is actually an issue. Most of the time you don't need it, and it definitely shouldn't be used for every query, but there are situations where it is necessary to keep the application at a usable speed.
Anthropic principle: We see the universe the way it is because if it were different we would not be here to see it.
The purpose behind stored procedures is not speed. It is about data integrity. Trigger functions are used to maintain additional coherence rules that are not easily expressed by unique, not null, referential integrity, etc. You place these rules in the database so that every consumer application of the database goes through the same logic--receives the benefits of the logic and enforces a coherent logic.
So answer me this: Now that you've made the investment to move to Oracle, can you reasonably foresee moving back to SQL Server? Seems unlikely to me. So if you'd chosen the right tool for the job in the first place, the migration wouldn't have been a problem and you would have been free to use other right tools where appropriate, e.g. stored procedures -- right?
Really, stored procedures have their place. Oracle tends to over-sell them, but to ignore them completely seems like one more step backward in the last n years of best practices and lessons learned.
Breakfast served all day!
Mod parent up!
Stored procedures have added benefits such as additional security, and forcing application developers to implement database functionality properly, not sloppily.
You know, I have one simple request. And that is to have sharks with frickin' laser beams attached to their heads!
Stored procedures should be used to enforce data integrity and data access. The only "business logic" that should be there is the logic that applies to keeping your data whole, complete, and coherent.
Middle-tier application layers are great to help pull data together, present model-specific views of data (say, OO/R mapping, if that is what gives you a chubby), and provide update interfaces. They can even do a first pass at making sure the database will accept the data.
But.
The data logic should be stored firmly in the database logic itself, using stored procedures and triggers and rules. RDBMS engines are *designed* to keep your data in good shape. Use them in that fashion, and you will be plenty happy. Don't, and you risk losing data integrity, and you sacrifice security.
Microsoft is to software what Budweiser is to beer.
I think part of postgresql's problem is its awful name. Just about anything would have been better (e.g. TurboSQL, AgileSQL, FastSQL, UglySQL, FatDumbAndHappySQL, etc).
And I would fire any moron who refused to use them. There are numerous reasons to use them. I find it scary that anyone employs you as a manager. You must be one of those PHBs from Dilbert.
age old concept - cheap, easy, simple thing that works. almost all tech stuff took off like that.
Read radical news here
It IS a little out of date, but the MySQL forums were dotted with little gems from the programming team like "if MySQL doesn't do it, you don't need it".
For a high-traffic fairly leisurely updating database where element veracity isn't CRITICAL, they were right.
What they didn't realise was that people on the forums didn't all work in areas where that was true and would prefer not to have to use two databases...
They're better, but then again, they're losing the edge on speed, too.
> As a manager now, I would fire anyone who uses stored procedures. Even if it is "faster."
And your boss would fire you pretty fast. But I doubt you've ever fired anyone over it or even threatened it. Pretty much everyone who pulls out this "I would fire anyone who xxx" gem is full of shit, has never managed anyone, never should, and thankfully never will.
Done with slashdot, done with nerds, getting a life.
'In some situations, the ability to quickly migrate to an alternate database outweighs the benefits that stored procedures may provide. In other situations, it doesn't.'
Doubtful. There are no shortage of individuals who find vendor lock to the worst possible scenario. There is nothing irrational about avoiding technology until it becomes standardized. Now that open applications implement them, the open applications will collaborate and develop a standard. Then there will be a certain level of standards compliant SPs you can use and still not be locked into a vendor. There will still no doubt be proprietary choices as well, the young and stupid will use them for their whizzbang features, the wise will avoid them until they become ubiquitous.
You know, I have one simple request. And that is to have sharks with frickin' laser beams attached to their heads!
well, you certainly sound like a manager.
You decide an entire methodology for development is stupid because of one bad experience and you would fire anyone who dare challenge your AUTHORITAY!
Is the CLI for Firebird as friendly as MySQL's? I've found most vendors' CLIs extremely unfriendly and limited compared to MySQL. Sort of like how Sun's unix utilities are largely crap compared to their GNU analogs.
Trying to show columns from table in Sybase? I'd rather stick a fork in my balls.
And if I were a manager, I'd fire ALL OF YOU for wasting time on a /. DBA pissing match instead of working.
Still, if you're developing a system only YOU as developer should have access to it. I have seen few such projects where that is the case and you still need Stored Procedures, Triggers or the like. It is nice to have if you (need to) let others have access to your database and you have a very archaic structure, but if your datastore is well thought out from the beginning and your programming not too complex, I would not fiddle around with it.
Custom electronics and digital signage for your business: www.evcircuits.com
"Uh, I can reinstall the procedures, I have the SQL Server CD with me."
"Get OUT."
Although it wasn't efficient, I hacked together a way to call "stored procedures" in MySQL and PHP. Essentially I had a function callSP. You passed in the name of the SP's and whatever parameters you needed (as a string). Then I'd use PHP to build the necessary query and return $result. It worked for whatever I was doing... although it wasn't a "true SP".
Vivin Suresh Paliath
http://vivin.net
I like
Sounds like they're violating a patent for "Method and Implementation of Field-Based Reality Distortion" held by Apple, Inc.
A few scenarios where on might worry about portability: (1) you're working on a system that you're hoping to get to work with whatever database the user has access to already, (2) you've gotten tired of being raped by Oracle's licensing, and you'd like to hold the door open to switching to another database, if only as a bargaining maneuver to push for discounts/reduced prices.
On the other hand, if you actually trust the culture of the developers working on the database you're interested in, you might very well choose to embrace the special features of that database, because the need to switch to another one at some point in the future seems very unlikely to occur.
(And if you ask me, this means "Postgresql". Trusting to Mysql culture does not sound like a great move to me.)
Also, I hear tell that some people work in places that are not 'states' at all!
(And I'm not talking about D.C.)
my_replace( 'We love the Oracle server', 'Oracle', 'MySQL').
The long Winter evenings must just fly by.
Demonstrably false. The ANSI standard for stored procedures already exists. MySQL has merely implemented this standard. You can port stored procedures to any other database that supports the standard (which admittedly didn't give you a lot of choice last time I looked). PostgreSQL initially took the "be compatible with Oracle route" with PL/pgSQL, so moving stored procedures between the two would be easy. They've since added the ability to have SPs in alternative languages, so it should be easy to add a PL/ANSI to PostgreSQL, assuming someone hasn't already done so (I haven't checked).
"The invisible and the non-existent look very much alike." -- Delos B. McKown
And then a few years ago I had a developer in one of my teams that was a freakin' SQL guru. I mean, this guy was just fantastic. He showed us how to do the most amazing things with SPs and functions without sacrificing speed, integrity or maintainability. I'm really grateful that I had the (humbling!) experience of working with him for a year, because I learned a hell of a lot about a topic that previously I had found mostly fastidious. Trust me that whetever we were paying him at the time it wasn't nearly enough.
I'll give you an example where inline SQL is not only problematic but simply just flat out impossible. At a previous project I was in charge of a rather large application for a financial services company that shall not be named at this time. Aside from actually designing and writing the thing, part of the mandate was to be able to pass a SOX audit with flying colors. The database for this app was secured so that the confgured identity we were using only had permission to execute SPs and views. No direct table access. This enables the architect (moi) to assert that there is a clear audit trail in the form of source control for the SPs and the database changelogs that can tell them (the auditors) at what point who decided that writing or reading from/to TableX or TableY was a good idea. In a scenario like this, not only would inline SQL simply fail for lack of permissions, but it would cause me a few week's delay and a good chunk of the client's budget (and likely my job thereafter) to fix because I'd fail the audit.
The world of enterprise corporate software development can be a bitch, but it pays very well =)
Web2.0: I love when people Flickr my cuil and digg my boingboing until my google is reddit and I start to yahoo
Why would you go with MySQL over Firebird?
Easy. The nimrod who wrote the application that you want/need to run didn't make the app database agnostic, so you are stuck with MySQL because "it's more popular". It's a catch 22. Until more folks start writing database agnostic apps, lots of us will get stuck using MySQL in places where we might prefer other databases. And so that will perpetuate MySQL being seen as being more popular... Argh.
Try PostgreSQL. The command in psql (CLI for Postgres) is a nice,
\d [table name]
or \l to list all tables in database.
Very handy and fast.
Firebird CLI (as of version 1.0.3 which is not a recent one but is what I use) is as cool to use as eating rocks. It made me have remember fondly the old Informix dbaccess utility...
"I think this line is mostly filler"
While they are certainly useful in certain situations, they are a pain in the ass to debug. It would be nice if they could add some sort of vendor specific command - anything - that would allow me to see output in a console while a stored procedure/function is running. They are useful at times, but i dread writing them.
Similes are like metaphors
'Demonstrably false.'
Okay, there is a standard... being adopted by open programs that will collaborate to implement that standard. This means choices and the ability to avoid vendor lock. I wasn't aware of the existing (unimplemented) standard but that doesn't change anything else I said. It is usage by a number of open, robust, and secure applications that makes it safe to adopt technology. Having the backing of a standards organization is just a bonus.
> Put it to you this way: If the features of MySQL are "good enough" for the application you want -- which arguably goes for
> most of the Web apps out there -- why would you not choose MySQL? There are times when swimming upstream is a noble
> effort, but generally all it gets you is tired.
Great point. Aside from asking what exactly "good enough" means - and pointing out how odd it is that some would insist on picking a tool because it's "good enough" while others insist on picking "the best tool for the job", a few possible reasons:
1. maybe you don't want to waste time testing for exceptions that should be reported in a more robust fashion
2. maybe you need a large reporting database - and don't want to waste $100k+ on extra hardware to make up for mysql's lack of partitioning, parallelism, automatic summarization and mature optimization.
3. maybe you need multiple databases, and one of them is a reporting database - so decide to go for a consistent other option to save on labor (which is more expensive that licensing costs these days)
4. maybe you want free online backups
5. maybe you want to avoid licensing costs
6. maybe you want to avoid having to talk to a lawyer to deal with mysql's obfuscated license
7. maybe you need better optimization for complex queries
8. maybe you want to ensure that clients can't override your data quality constraints
9. maybe you find that there are many great programmers who would prefer not to work with mysql
10. etc,etc,etc
So, quite a few reasons why a person might think that mysql has a way to go before being good enough for their project.
in postgres:
\d <table_name>
pretty easy.
I was working for a startup in 2002... we had to cut costs anywhere we could so we dumped Oracle licensing. Me and another guy were able to convert the entire website and all our internal applications over the course of a day.
We were using EJBs without stored procedures and the only reason it took as long as it did was because some of the more exotic queries we were using at the time.
How about data mining? You need access to the data. Developers are NOT the only people with access to the database. If they are, why the hell use SQL? There are much faster databases.
As a manager now, I would fire anyone who uses stored procedures. Even if it is "faster."
It is possible, and in some cases effective, to split a system's logic into presentation logic, business logic, and data access logic. If you do that, it is possible to write a good three tier system that has some of the logic in the database (I'm referring to the data access logic - for the slow folks in the crowd).
I'm not saying that every database backed system should be this way. Nor even that every database backed system will benefit from it. But there are some systems that can benefit from it. This is particularly true when the ability to migrate between RDBMSs is a very low priority (or claimed to be a non-priority), and the decoupling of the data access logic from the business logic is a very high priority.
Rules of thumb are nice. Absolute laws are the enemy of pragmatic system design.
Stop-Prism.org: Opt Out of Surveillance
> I would like to know what slashdotters think is a good level of stuff to be putting into stored procedures.
Here's an example:
On a current project I've got a separate department of users who have written a reporting app in actuate against a db2 reporting database (a datamart). Along the way we've found that this team is not highly skilled in sql and often had to rewrite some of the sql to fit it into their tool. Note that this can be relatively complex sql - involving temp tables, etc - in order to show trends. The results have been queries that returned incorrect data, that scanned 4 *trillion* rows over 6 hours rather than 1 million in 5 seconds, etc, etc, etc.
The solution that we settled upon was to encapsulate all of their sql within stored procedures. These procedures then:
1. validated all arguments - to ensure that they didn't mix them up and ask for the wrong data
2. logged each call along with argument values, rows returned, and time to return
3. returned the result set along with some useful metadata
4. processed everything in a highly consistent way
Now, I don't typically use stored procedures heavily - and often prefer to encapsulate the physical data model in views to save time. But in this case the availability of this option was really a life-saver. And note that these stored procedures are also allowing us to more easily change the underlaying data model, measure and tune each query, maintain the queries, etc, etc.
All good stuff, though your mileage may vary.
Would you believe it, most places in the world are not in any of the states within the USA!
Try firing an employee without a previous written warning anywhere in the EU and you'll find yourself in a spot of trouble.
Don't worrie, I am well aware of corporate software development. I am one of the primary developer for one of the largests non-software companies in the world :) We also have an army of DBAs and database-specific developers, and right now since it IS a stored-procedure-only environment, we have (just on the part I work with) several -douzans of thousands- stored procedure, that do everything you can imagine (including non-database related tasks). I've seen, or at least heard, most everything they can do, no worries :)
.NET's silly typed dataset allows you to switch between inline SQL and stored procedures completly transparently (though using inline SQL in a .NET dataset is unmaintainable, so thats probably a bad example :) )
Optional parameters and parametrised order bys are the least of my worries here (though having a stored procedure with a dynamic amount of fields, multi-dimentational arrays of parameters, and dynamic order by clauses that allow to sort by a varying amount of fields end up being quite long to make, no matter the magic you apply to them. Easy to write, don't get me wrong, but...). And even with all that, I could just use, for example (on SQL Server), a CLR stored procedure, and even those problems would go away. Thats not the issue at hand.
The issue is that with a well thought out abstraction layer and automated scripts on top of the less critical parts of the applications, you can decouple your model and your database in ways that even a full layer (or two) of views and stored procedures simply can't do.
I mean, great: today my data is coming from 6 datacenters, using several different RDBMS, 2 different ETL technologies, and (thank god) only 1 OLAP system. Thats cute. If tomorrow I decide to change something in there....well, stored procedures aren't the most abstracted thing in the whole wide world. If I upgrade, let say, a SQL Server 2000 to 2005, well, the way to handle something as silly as filtering/paging efficiently changed. Now I have to go through the 4 thousand SPs that used the SQL Server 2k crippled way and optimise them? Hell no, that will probably be simply forgotten and never done. If it ain't broken don't fix it. If I have an SQL abstraction layer, I'll just change 2-3 functions, pass them through the unit and integration tests, and if everything comes up green the douzans of apps in my company will all take advantage of it by the next day.
On top of that, there's the notion that some things are simply easier to think of in an object oriented way, and it just feels real, REAL dumb to have 4 stored procedure for every god damn definition table (thousands!) in there. 90% of those won't be holding money amounts nor credit card numbers, and will be hiding behind locked down web services or remoting APIs -anyway-, so its not like anyone is getting direct access to the tables either. And if thats troublesome anyway (because of the risk of failure of these layers), you still have your views. And if thats still not enough, THEN of course (like in the example you gave), you go the stored procedure way, in which case the SP ends up being just an alternate datasource, the same way my ETLs and OLAP providers are. No biggy: even
What I'm getting at, is that (again, as your example shows), in corporate environments, doing without stored procedure is virtually impossible. No argument there! You -need- them. However, using ONLY stored procedures is simply a disaster waiting to happen, on top of being a hell of a waste of man hours. Some stuff is simply 10x faster to do using a well made SQL abstraction API. The important part is to always have a good way of using the stored procs that will come in.
I'll have to put on my flame suit for this one only because so many interests are out here defending non-defendable territory.
.... then turn tail and try to put business logic into the database tier. Try moving a large MS-SQL app that utilized stored procedures to Oracle or MySQL. Tell the wife you'll see her next year!
Why does everyone love CSS for separating content from format
Presentation - Logic - Storage. Clean and simple. Stored procedures and triggers are nothing more than DB-CRACK. Easy to use in a "pinch" yet keeps you addicted forever.
The concept of moving trigger/stored procedure coding out of the database engine is the scariest thought for Oracle/MS/IBM....yet it works surprisingly well in the freebie MySQL.
This subject is so deep and broad it leaves the mega dollar database companies room for more FUD the HS in NYC in 1905 (check that one out in your database).
SCALE THAT IN YOUR BOARDROOM!
Gizmos Gagets For Ninjas
Kudos to you Shielfw0lf. I worked on an enterprise project using MySQL. I'd used it before on hobbyist/home-project/web-sites and thought it was waaaaaaaaay cool. But when we started working on, man, we found it so painful. Often The indexing system is insane (only one index can be used per query), and it was very buggy. MySQL InnoDB was better, but even it suffered from 'epic rollbacks' that would take hours. Things often wouldn't work properly: You'd find simple expressions that wouldn't evaluate properly. When we reported them to MySQL they were very arrogant about it. After that experience, I'd never try and use MySQL in an enterprise application (defined as anything critical that your life or money hangs off). A friend of mine bought out another business. During negotiations the seller said my friend should buy 'so you have a backup when your databases go down'. Huh? said my friend, 'my databases don't go down. ever!' Turned out the seller used MySQL. My friend used Oracle. Of course Oracle has its problems (absurdly expensive and prehistoric tools), IBM DB2 and Microsoft SQL are both dogs. Firebird DB is nice, free and open source based on Borland's Interbase DB.
For small projects and personal web sites, MySQL is fine. You don't get cheaper than free and because it's so widely used never a problem finding people that know how to use it. If I had to do it all again: Firebird or Oracle.
Would you believe it, most places in the world are not in any of the states within the USA
Yes, but Pax Imperium will be coming soon to liberate a country near you.
Necessity is the mother of invention.
Laziness is the father.
Do you even know what a 'nested-loop join' is? Hint: It happens inside the RDBMS if the optimizer can't figure out a better way of joining two tables.
"Put it to you this way: If the features of MySQL are "good enough" for the application you want -- which arguably goes for most of the Web apps out there -- why would you not choose MySQL? There are times when swimming upstream is a noble effort, but generally all it gets you is tired."
Yes. But here the problem seems to be to decide what direction the water goes.
I'll rework your question: If you expect to develop in any reasonable future a database-driven app that might need proper RBDM functionality -- which arguably goes for most of the people earning a life on the "informatics thingie" -- why would you choose to thrash away time learning a tool you will have to abandon in the near future when the "proper" tool (ie: postgresql or firebird) will serve you in your hard tasks as well as in your easier ones with any drawback?
Or you will blow every schedule you've got.
DBAs are administrators. They write backup scripts etc. A good one will be capable of writing scripts to update data structures without using tools like ERwin. (ERwin and the like are the better way to do it you understand what the tools are actually doing. Very few DBAs have that depth of knowledge.)
The DBAs also think they are the only people that understand how databases function. (Often they don't even understand, but when has that stopped anyone.)
They are also usually very anal. That's what makes them good admins.
If you need to update complex SQL on a regular basis you embed it in YOUR source, send it to the server and eat the repeated compile time.
The alternative is that your SQL becomes part of the DBAs schema (or worse yet data model) and you need an act of congress to be able to make any changes.
Granted there comes a point where everything has stabilized where it makes sense to let the DBAs pretend they're in control.
John McAfee 'It was like that time I hired that Bangkok prostitute; to do my taxes, while I fucked my accountant'
So we can not apply there and short your companies stock!
Asshat.
John McAfee 'It was like that time I hired that Bangkok prostitute; to do my taxes, while I fucked my accountant'
Bull.
Stored procedures are extremely useful for security, to reduce network traffic to the client or application server, to ensure consistency, to take advantage of database-specific features, and most importantly to ensure data integrity.
Fair enough. But I would answer it this way: Anybody who spends a lot of time fretting over whether to learn Java or C# is missing the point. If you understand programming, it's not hard to learn both. And while you're at it, you should also pick up C and C++, in addition to Python and/or Perl. And whatever else you feel like. Experiment with things like functional languages while you're at it. These are all just tools. Some university-level computer science programs don't teach any programming languages. The language you use to implement your ideas is a secondary concern.
... you're taught SQL. And since a lot of SQL is nonstandard, that means you learn PL/SQL or T-SQL. I'd argue that this is the wrong way to do it. There's a lot of science behind relational databases dating back decades, and DBAs too often ignore a lot of the best practices because they don't really understand relational theory and how databases work.
Similarly, the field of database design is choked with so-called DBAs who learned their trade using one tool to the exclusion of all others. Most of them can be forgiven for this, because that's how many database classes and books approach the problem. You're not taught databases
So back to your point: Sure, anybody who really understood database theory might have such a strong adverse reaction to MySQL that they'd never use it for anything. Then again, if they really know what they're doing that well, then they'll probably understand the application they're trying to build well enough to know if it's likely to grow beyond the capabilities of MySQL. If it's not likely to do so, then they might make the conscious decision to use the more popular tool because of its wide availability, strong community and ready support. To me, an Oracle DBA who doesn't also learn how to use MySQL is just being narrow-minded. (He should also understand the drawbacks and advantages of DB2 and MS-SQL, too, whether or not he ever intends to contract out to work on them.)
Or, to put it another way: When the only tool you have is a hammer, every job starts to look like a nail. And treating them that way might work, superficially, but there are all kinds of nails out there. It's not always about bringing the heaviest hammer you can find.
Breakfast served all day!
http://tweakers.net/reviews/649/7 Tweakers is a dutch community of online tweakers that was deciding on new hardware. The above link leads you to some stunning performance graphs that show that not only postgresql 8.2 is faster then MySQL 5.0, it also scales MUCH better on heavy loads! This benchmark was discussed earlier on slashdot to some degree, but I think quite a few of the commenters above have not seen this. The tweakers community are longterm MySQL users and were new to PostgreSQL. However they even brought in a MySQL expert to configure it optimally. Still PostgreSQL turned out to be the better dbms
Hello,
I worked a lot with MySQL stored procedures lately.
While handy for small and simple things, MySQL stored procedures aren't that great for more complex processing.
- Procedures are difficult to debug. There's no debugger and no way to trace what's going on except by using horrible tricks.
- The language is somewhat limited. Doing very simple things can require dozen of lines, with juggling between local and thread-local variables. And since there's nothing like arrays or associative arrays, you have to use temporary or MEMORY tables to do the job, and it implies some more horrible tricks.
- It's slow. Slow, Damn slow.
This is why UDFs are still the way to go for any complex task.
{{.sig}}
The only reason to go with MySQL instead of FB is that you have to grok different kinds of transactions in Firebird whereas in MySQL you can just push your luck. MySQL will (almost) never show you something about conflicts or deadlocks. Yes, you will end up with mess in your data but at least your users won't see an exception/error thrown at them, nice, eh? All other things - like performance, capacity etc - Firebird beats MySQL or is at least on par.
PS: Oh, and Firebird used to have slightly less effective transport protocol to remote hosts (over TCP/IP) but it has been worked on and I have no fresh data on present state of it. MySQL just sends an SQL query to the server and gets the data back. In best case it's just 3 IP packets (query, confirmation/answer_data, confirmation), FB is a bit more complicated. Anyway - it's not a biggy.
PPS: And MySQL has some basic full-text search built-in. For FB you need to code it yourself or get a third-party solution. If you need it.
The links you provide are to a very old version of Mysql (3.2xxx), which was current around 6 years ago.
The newer Mysql version, depending on which storage engine you use, support foreign keys: Version 5.1
I've used stored procedures in Mysql, and have to say that they are very limited for anything complex. There are a lot of limitations in both stored procedures and functions which make them almost unusable for real tasks. The limitations have mainly to do with what you can pass to the procedures and functions and what you can return.
I ran into exactly the same problems as you did when trying to write SPs that would do maintenance on other tables. It simply was not worth the hassle. The fact that I had to resort to temporary tables because there are no associative arrays (Postgres, thank god, does have associative arrays), was the biggest problem.
Mysql is getting better, but I still think there's only Postgres as a real alternative to things like Oracle, mainly because the feature sets between the two are so similar.
It's definitely worth checking out if you use MySQL. It's well written and comprehensive. My own review was very close to this one. You can check it out on my blog (or amazon).
"MySQL has some basic full-text search built-in. For FB you need to code it yourself or get a third-party solution."
Firebird has Interbase's LIKE, CONTAINING, and STARTING WITH clauses, all of which are specifically designed for pattern-based full text searches. For example:
"select * from people where last_name like 'Ma%' "returns all records where the "last_name" begins with the letters "Ma".
"select * from people where last_name starting with 'Ma'" is functionally equivalent to the above.
"select * from people where last_name containing 'Ma'" performs a case-insensitive search for records where the two characters occur anywhere in the "last_name" field.
The above clauses can be combined using AND and NOT to perform fairly sophisticated text searches. Interestingly, CONTAINING can also be used with non-text fields as well, and the same may be true of the other the other two (I'm not sure if this is actually the case, though).
I'm not going to change your sheets again, Mr. Hastings.
Nope. That would be the Abrams.
Only three things are certain; death, taxes, and apocryphal quotations - Ben Franklin.
Yeah, thought I'd hear those old chestnuts trotted out. They're the ancilliary benefits I mentioned.
Deleted
1) Crew get out, except the thinnest one
2) He drives it over the bridge
3)
4) Remainming crew walk across bridge one at a time & get back in tank
It's true I tell you, feller at work's next door neighbour read it in the paper.
Doing that is awfully slow for a database that is bigger than tiny. To get decent performance you need some sort of indexing, which is what I think the other poster was refering to.
MySQL has a fulltext index feature (only for MyISAM tables currently) that does just that. Most other RDBMS also have similar.
Firebird still hasn't any, thus applications need to build and keep their own indexes, which is often slower and requires more storage space than the RDBMs integrated fulltext search indexes.
Certainly, what works with one solution doesn't necessarily have to work with all the others. I've been in this business long enough that I recognize I sure as hell don't have all the answers, and more importantly, I have ceased to be amazed at the crap I see in companies of all types and sizes. The best tools for the job and all that.
Cheers.
Web2.0: I love when people Flickr my cuil and digg my boingboing until my google is reddit and I start to yahoo
"Doing that is awfully slow for a database that is bigger than tiny"
I have two real-world application with pretty large databases that return result sets from text searches on blobs in a second or so. One is an application running on an old Interbase 4.2 with a fairly large number of simultaneous users and around 1/2 million records which has been in daily use since the 1990s, and is constantly being updated from several simultaneous news feeds, so the contents are constantly changing. Another heavily deployed medical system from a company I'm involved with uses Firebird with a fairly complex (several hundred tables, some with more than 100 fields) database. The type of searches being performed (case insensitivity is important) mean that the "CONTAINING" predicate is in all cases, and this could explain the fairly good performance -- "LIKE" for example is _much_ slower (although I haven't bothered to benchmark the differences on these data sets because I'm a programmer, not a reviewer).
"which is what I think the other poster was refering to"
If that's what he meant, then he should have said so in his post.
"MySQL has a fulltext index feature (only for MyISAM tables currently) that does just that"
Unfortunately, it's implemented sub-optimally. The system works well if the entire text index fits in RAM and no result sorting is required, but degrades significantly when this is not the case (sometimes _very_ significantly). As with many other things in MySQL, it's an excellent mechanism for those whose requirements are within a set of fairly narrow boundaries, but falls well short of being a generic solution for high performance text searching.
"Most other RDBMS also have similar"
Oracle has this facility, SQL-Server, Informix, and DB2 don't (SQL-Server and DB2 use external tools to build and maintain indices, one via the OS services, the other with a supplied proprietary program). If 1/4 counts as "most", then you are correct that "most" other RDBMS systems do indeed have this facility!
"Firebird still hasn't any, thus applications need to build and keep their own indexes"
My experience indicates that performance is more than adequate without them for records containing text data of 64K / field (document size before being placed in the database itself -- IB / FB have compressed large VARCHAR / text blob fields for well over a decade) in real-world applications with a significant data-set using CONTAINING. Performance will obviously degrade notably as field sizes grow, as of course would be the case with queries across several fields and / or tables because the volume of data that needs to be searched is obviously greater, and as I said previously, LIKE is reportedly a _lot_ slower.
"applications need to build and keep their own indexes, which is often slower and requires more storage space than the RDBMs integrated fulltext search indexes."
While this is true, the same can be said for those using Informix, SQL-Server, and DB2, and indeed MySQL installations whose text querying requirements are beyond the capabilities of the internal system to handle efficiently (or handle at all). Thus, while MySQL's text indexing is certainly a point in its favour, the limitations and performance caveats of its implementation mean that unlike for example Oracle's CTXRULE and CONTEXT indices, it doesn't always eliminate the need for an external text indexing and search system.
I'm not going to change your sheets again, Mr. Hastings.
Sorry this is so late, but it's really just:
Good points. That is very true. I deal with data-critical applications (used to be medical/financial, now it's satellite data). So, I do have a specific viewpoint.
RDBMSs are good general tools, and often just storing the data is enough. Sometimes I forget that.
Microsoft is to software what Budweiser is to beer.