Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Constraints as a performance optimization
Old 07-08-2008, 02:32 PM Constraints as a performance optimization
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
I wonder how many people are already aware of this, and practice it? I wonder if MySQL is capable of this?

Constraints are a way of enforcing data integrity. If your data is of value, they prevent it from going corrupt, becoming untrustworthy. There are different types, from default values to checks to referential integrity. All of these are declarative - you tell the database what the rules of proper data are, and it makes sure they're obeyed.

A check constraint is simple. If you have an employee table with an age column, you might only allow values from 18 to 80 years old. If somebody tries to update an employee and make her 175 years old, the database would fail that update - if you have a check constraint in place. Here's the kicker, if you query the employee table for anyone > 80 years old, it will return an empty set without even looking at the table!

DRI is similar. If there's a primary and foreign key relationship between two tables, you can't have data in one unless there's matching data in the other. With this setup, if you query for line items for an order that doesn't exist, the database will return an empty set, often by looking at the order table instead of order detail. Why? The table is much smaller, so you get your answer much faster.

You can verify this by looking at query plans. And then, so long as you don't use nocheck, you can optimize read performance in your database by setting up proper constraints.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-08-2008, 07:51 PM Re: Constraints as a performance optimization
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,528
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
MySQL has had constraints since ver 3.23, but only if you use InnoDB tables
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 07-09-2008, 03:44 PM Re: Constraints as a performance optimization
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
But does the MySQL query optimizer consider (trusted) constraints to narrow the execution plan?

And are you serious - a database engine really only has something as central as constraints with one particular storage type?
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 07-10-2008, 05:32 AM Re: Constraints as a performance optimization
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,528
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
And are you serious
Yep, but to be fair to the MySQL developers, the MyISAM tables were/are only meant for relatively simple and basic applications, where all the bells and whistles were not really needed but speed is a concern.

If the database design or the application of the database called for the more "advanced" features then the storage engine of choice should be InnoDB, or change servers to PostgreSQL.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 07-10-2008, 03:51 PM Re: Constraints as a performance optimization
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
Wow. I learn something every time I log in here. Anyway, my point wasn't to bash MySQL. I'm grateful for the knowledge I pick up, but I was actually trying to give some of that back, instead of just troll.

Does anybody know whether the MySQL query optimizer uses constraints (check, PK & FK, etc) to make better decisions about how and in what order it will execute the query?

Any type of constraint is going to slightly slow down insert and update queries, and we've had several data warehouse projects where we disable them (especially DRI) to improve ETL performance. That's a special situation, because foreign keys are implied in a DW. But perf can arguably be a reason not to constrain your data, if you have some other validation method in place. But, if those constriants give the query optimizer more ways to narrow the work that needs to be done to satisfy a query, there are specific cases where they can give substantial performance improvements. So I was hoping to spark a discussion about when it makes sense to use them, in this context?
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Constraints as a performance optimization
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.23544 seconds with 12 queries