+ Reply to Thread
Results 1 to 8 of 8

Thread: SQL Database Optimization

  1. #1
    batman1's Avatar
    batman1 is offline x10Hosting Member batman1 is an unknown quantity at this point
    Join Date
    Aug 2008
    Location
    Jamaica
    Posts
    92

    Arrow SQL Database Optimization

    Should I add and INDEX onto a date "Y-m-d" field, even though many records can have the same date.

    I run a query that check all those records with last login which is today.

    Please reply. :cool:

    Points for helpful help.

  2. #2
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: SQL Database Optimization

    I personally don't add any index fields except for the primary field. Unless you have 10 000 rows, it won't make your queries faster. It will even slow them down.
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  3. #3
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: SQL Database Optimization

    It's rather hard to answer this sort of question given so little information. Please post:
    • the tables' definitions,
    • the indices already defined on the tables ("SHOW INDEX FROM <table>;"),
    • the query you run, and (if it's a SELECT query)
    • the output of "EXPLAIN <query>" (2, 3), where <query> is your select statement. This can also be used to refactor the select statement.
    Keep in mind that indices can speed up selects but will slow down any query that changes the data in the tables (inserts, updates and deletes). Usually you get data from a table more often than you change it, but if this isn't true an index may not help overall.

    Alternatively, you could take the experimental approach. Get the timing from running the query a few thousand/million times without the index, and then with the index. If it's faster without, drop the index.
    Last edited by misson; 06-20-2009 at 04:08 PM.

  4. #4
    batman1's Avatar
    batman1 is offline x10Hosting Member batman1 is an unknown quantity at this point
    Join Date
    Aug 2008
    Location
    Jamaica
    Posts
    92

    Re: SQL Database Optimization

    Quote Originally Posted by misson View Post
    It's rather hard to answer this sort of question given so little information. Please post:
    • the tables' definitions,
    • the indices already defined on the tables ("SHOW INDEX FROM <table>;"),
    • the query you run, and (if it's a SELECT query)
    • the output of "EXPLAIN <query>" (2, 3), where <query> is your select statement. This can also be used to refactor the select statement.
    Keep in mind that indices can speed up selects but will slow down any query that changes the data in the tables (inserts, updates and deletes). Usually you get data from a table more often than you change it, but if this isn't true an index may not help overall.

    Alternatively, you could take the experimental approach. Get the timing from running the query a few thousand/million times without the index, and then with the index. If it's faster without, drop the index.

    your post has been helpful thanks for the reply.

  5. #5
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: SQL Database Optimization

    Could you still post the results for the benefit of the community?
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  6. #6
    vol7ron's Avatar
    vol7ron is offline x10 Lieutenant vol7ron is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    DC
    Posts
    434

    Re: SQL Database Optimization

    Quote Originally Posted by xav0989 View Post
    ...Unless you have 10 000 rows, it won't make your queries faster. It will even slow them down.
    Quote Originally Posted by misson View Post
    Keep in mind that indices can speed up selects but will slow down any query that changes the data in the tables (inserts, updates and deletes). Usually you get data from a table more often than you change it, but if this isn't true an index may not help overall.
    Wrong and wrong.

    The more records a table has, the more important indices become and yes they do work against smaller tables because the database first scans the data library and finds the index and then scans the physical table for data. If the original table is small enough, then it's faster to perform one table scan then two, but you can't put a number on how many records this occurs at.

    The second point is wrong because indices are used and help improve performance on Updates and Deletes in addition to Select queries. They are generally not used on Inserts unless a table join or conditional statement exists because Interts just append a new row to the end of a table.

    The important thing to take away is that large tables need indices - I'd put the number starting around 2500 records even though as i said before, there is no real number (especially depending on hardware). But the most important thing to remember is that the only fields that need to be indexed are the ones used in a WHERE clause of a query. Once you set that up, determining what type of index to use (b-tree, etc) is the next step to performance tuning.
    Last edited by vol7ron; 06-21-2009 at 03:26 PM.
    If you find my post useful please add to my reputation by clicking the +Rep button
    You may also use the Donate link to donate credits - this is appreciated too Thanks to those whom have donated so far!


  7. #7
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: SQL Database Optimization

    Quote Originally Posted by vol7ron View Post
    The second point is wrong because indices are used and help improve performance on Updates and Deletes in addition to Select queries. They are generally not used on Inserts unless a table join or conditional statement exists because Interts just append a new row to the end of a table.
    You're right in that indices can help with UPDATEs and DELETEs. However, there is a cost to using indices, including the need to update them, which will negatively impact performance. Generally, the trade-off results in a net gain.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  8. #8
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: SQL Database Optimization

    Quote Originally Posted by vol7ron View Post
    Wrong and wrong.

    The more records a table has, the more important indices become and yes they do work against smaller tables because the database first scans the data library and finds the index and then scans the physical table for data. If the original table is small enough, then it's faster to perform one table scan then two, but you can't put a number on how many records this occurs at.

    The second point is wrong because indices are used and help improve performance on Updates and Deletes in addition to Select queries. They are generally not used on Inserts unless a table join or conditional statement exists because Interts just append a new row to the end of a table.

    The important thing to take away is that large tables need indices - I'd put the number starting around 2500 records even though as i said before, there is no real number (especially depending on hardware). But the most important thing to remember is that the only fields that need to be indexed are the ones used in a WHERE clause of a query. Once you set that up, determining what type of index to use (b-tree, etc) is the next step to performance tuning.
    What I like about the internet is that you always learn something. Just now, I've learned that something that I thought was true for a long time. I won't sleep as dumb tonight! (no sarcasm)
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

+ Reply to Thread

Similar Threads

  1. Need Help To Reset SQL Database to 3
    By shutlanz in forum Free Hosting
    Replies: 2
    Last Post: 11-09-2008, 12:40 PM
  2. Transferring SQL Database
    By Cavanaugh Corporation in forum Free Hosting
    Replies: 0
    Last Post: 08-16-2008, 07:50 PM
  3. sql database does not restore
    By softwork in forum Free Hosting
    Replies: 3
    Last Post: 05-20-2008, 06:47 AM
  4. SQL Database location?
    By martynball in forum Programming Help
    Replies: 4
    Last Post: 05-15-2008, 12:20 PM
  5. SQL database
    By holeepassion in forum Free Hosting
    Replies: 2
    Last Post: 05-06-2008, 12:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers