SlideShare uma empresa Scribd logo
1 de 33
Baixar para ler offline
10 sql tips to speed up your database | CatsWhoCode.com




                                               10 sql tips to speed
                                               up your database
                                               On most websites, content is stored in a
                                               database and served to visitors upon
                                               request. Databases are very fast, but there’s
                                               lots of things that you can do to enhance its
                                               speed and make sure you won’t waste any
                                               server resources. In this article, I have
                                               compiled 10 very useful tips to optimize and
                                               speed up your website database.


                                               Design your database
                                               with caution
                                               This first tip may seems obvious, but the fact
                                               is that most database problems come from
                                               badly-designed table structure.
                                               For example, I have seen people storing
                                               information such as client info and payment
                                               info in the same database column. For both
                                               the database system and developers who
                                               will have to work on it, this is not a good
                                               thing.
                                               When creating a database, always put
                                               information on various tables, use clear
                                               naming standards and make use of primary
                                               keys.
                                               Source:                http://www.simple-
                                               talk.com/sql/database-
                                               administration/ten-common-
                                               database-design-mistakes/


                                               Know what you
                                               should optimize
                                               If you want to optimize a specific query, it is
                                               extremely useful to be able to get an in-
                                               depth look at the result of a query. Using the
                                               EXPLAIN statement, you will get lots of
                                               useful info on the result produced by a
                                               specific query, as shown in the example



http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
10 sql tips to speed up your database | CatsWhoCode.com

                                               below:




                                                  EXPLAIN SELECT * FROM
                                                  ref_table,other_table WHERE
                                                  ref_table.key_column=other_table.co
                                                  lumn;



                                               Source:
                                               http://dev.mysql.com/doc/refman/5.0/e
                                               explain.html


                                               The fastest query… Is
                                               the one you don’t
                                               send
                                               Each time you’re sending a query to the
                                               database, you’re using a bit of your server
                                               resources. This is why, on high traffic sites,
                                               the best thing you can do in order to speed
                                               up your database is to cache queries.

                                               There’s lots of solutions to implement a
                                               query cache on your server. Here are a few:

                                                      AdoDB: AdoDB is a database
                                                    abstraction library for PHP. It allows you
                                                    to use the database system of your
                                                    choice (MySQL, PostGreSQL, Interbase,
                                                    and way much more) and it is designed
                                                    for speed. AdoDB provides a simple,
                                                    yet powerful caching system. And last
                                                    but not least, AdoDB is licenced under
                                                    the BSD, which means that you can use
                                                    freely on your projects. A LGPL licence
                                                    is also available for commercial
                                                    projects.
                                                      Memcached: Memcached is a
                                                    distributed memory caching system
                                                    which is often used to speed up
                                                    dynamic database-driven websites by
                                                    alleviating database load.
                                                      CSQL Cache: CSQL Cache is an
                                                    open-source data caching infrastructure.
                                                    Never tested it personally, but it seems
                                                    to be a great tool.




http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
10 sql tips to speed up your database | CatsWhoCode.com


                                               Don’t select what you
                                               don’t need
                                               A very common way to get the desired data
                                               is to use the * symbol, which will get all
                                               fields from the desired table:




                                                  SELECT * FROM wp_posts;



                                               Instead, you should definitely select only the
                                               desired fields as shown in the example
                                               below. On a very small site with, let’s say,
                                               one visitor per minute, that wouldn’t make a
                                               difference. But on a site such as Cats Who
                                               Code, it saves a lot of work for the
                                               database.




                                                  SELECT title, excerpt, author FROM
                                                  wp_posts;




                                               Use LIMIT
                                               It’s very common that you need to get only a
                                               specific number of records from your
                                               database. For example, a blog which is
                                               showing ten entries per page. In that case,
                                               you should definitely use the LIMIT
                                               parameter, which only selects the desired
                                               number of records.
                                               Without LIMIT, if your table has 100,000
                                               different records, you’ll extract them all,
                                               which is unnecessary work for your server.




                                                  SELECT title, excerpt, author FROM
                                                  wp_posts LIMIT 10;




                                               Avoid queries in
                                               loops
                                               When using SQL along with a programming
                                               language such as PHP, it can be tempting to
                                               use SQL queries inside a loop. But doing so


http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
10 sql tips to speed up your database | CatsWhoCode.com

                                               is like hammering your database with
                                               queries.
                                               This example illustrates the whole “queries
                                               in loops” problem:




                                                  foreach ($display_order as $id =>
                                                  $ordinal) {
                                                      $sql = "UPDATE categories SET
                                                  display_order = $ordinal WHERE id
                                                  = $id";
                                                      mysql_query($sql);
                                                  }



                                               Here is what you should do instead:




                                                  UPDATE categories
                                                      SET display_order = CASE id
                                                          WHEN 1 THEN 3
                                                          WHEN 2 THEN 4
                                                          WHEN 3 THEN 5
                                                      END
                                                  WHERE id IN (1,2,3)



                                               Source:
                                               http://www.karlrixon.co.uk/articles/sq
                                               multiple-rows-with-different-values-
                                               and-a-single-sql-query/


                                               Use join instead of
                                               subqueries
                                               As a programmer, subqueries are something
                                               that you can be tempted to use and abuse.
                                               Subqueries, as show below, can be very
                                               useful:




                                                  SELECT a.id,
                                                      (SELECT MAX(created)
                                                      FROM posts
                                                      WHERE author_id = a.id)
                                                  AS latest_post FROM authors a



                                               Although subqueries are useful, they often
                                               can be replaced by a join, which is definitely



http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
10 sql tips to speed up your database | CatsWhoCode.com

                                               faster to execute.




                                                  SELECT a.id, MAX(p.created) AS
                                                  latest_post
                                                  FROM authors a
                                                  INNER JOIN posts p
                                                      ON (a.id = p.author_id)
                                                  GROUP BY a.id



                                               Source: http://20bits.com/articles/10-
                                               tips-for-optimizing-mysql-queries-
                                               that-dont-suck/


                                               Be careful when using
                                               wildcards
                                               Wildcards are very useful because they can
                                               substitute for one or more characters when
                                               searching for data in a database. I’m not
                                               saying that you shouldn’t use them, but
                                               instead, you should use them with caution
                                               and not use the full wildcard when the prefix
                                               or postfix wildcard can do the same job.
                                               In fact, doing a full wildcard search on a
                                               million records will certainly kill your
                                               database.




                                                  #Full wildcard
                                                  SELECT * FROM TABLE WHERE COLUMN
                                                  LIKE '%hello%';
                                                  #Postfix wildcard
                                                  SELECT * FROM TABLE WHERE COLUMN
                                                  LIKE 'hello%';
                                                  #Prefix wildcard
                                                  SELECT * FROM TABLE WHERE COLUMN
                                                  LIKE '%hello';



                                               Source:    http://hungred.com/useful-
                                               information/ways-optimize-sql-
                                               queries/


                                               Use UNION instead of
                                               OR
                                               The following example          use   the   OR
                                               statement to get the result:


http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
10 sql tips to speed up your database | CatsWhoCode.com




                                                  SELECT * FROM a, b WHERE a.p = b.q
                                                  or a.x = b.y;



                                               The UNION statement allows you to
                                               combine the result sets of 2 or more select
                                               queries. The following example will return
                                               the same result that the above query gets,
                                               but it will be faster:




                                                  SELECT * FROM a, b WHERE a.p = b.q
                                                  UNION
                                                  SELECT * FROM a, b WHERE a.x = b.y



                                               Source:
                                               http://www.bcarter.com/optimsql.htm


                                               Use indexes
                                               Database indexes are similar to those you
                                               can find in libraries: They allow the database
                                               to find the requested information faster, just
                                               like a library index will allow a reader to find
                                               what they’re looking for without loosing time.
                                               An Index can be created on a single column
                                               or a combination of columns in a database
                                               table. A table index is a database structure
                                               that arranges the values of one or more
                                               columns in a database table in specific
                                               order.

                                               The following query will create an index on
                                               the Model column from the Product table.
                                               The index is called idxModel:




                                                  CREATE INDEX idxModel ON Product
                                                  (Model);



                                               Source:               http://www.sql-
                                               tutorial.com/sql-indexes-sql-tutorial/




                                               More posts about SQL

http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
10 sql tips to speed up your database | CatsWhoCode.com

                                                  Getting started with CouchDB: a beginner’s gu


                                               Share this article
                                                            like
                                                            http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database
                                                            AVqRj-yi



                                                 387                49                          20
                                                 tweets
                                                                                                1
                                                retweet            Like
                                                                  Like




                                               Comments (54) - Leave
                                               yours
                                                              Bart Jacobs said: March 8,
                                                              2010 at 11:49 am
                                                              This article is very useful for
                                                              anyone working with mySQL
                                                              (and    with     databases in
                                                              general). For high traffic
                                                              websites especially, database
                                                              queries can be the bottleneck
                                                              (and often are). Thanks for
                                                              this great post!

                                                                                                                     Reply



                                                              Can Aydoğan said: March
                                                              8, 2010 at 1:27 pm
                                                              Great article. Thanks for
                                                              sharing!

                                                                                                                     Reply




http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
10 sql tips to speed up your database | CatsWhoCode.com

                                                            Metric Stormtrooper said:
                                                            March 8, 2010 at 3:27
                                                            pm
                                                            your table type can also make
                                                            a lot of difference. MyISAM is
                                                            better if you expect a lot of
                                                            reads, InnoDB if you need
                                                            more writes in a table (see
                                                            also
                                                            http://www.softwareprojects.com
                                                            mysql-storage-engines-
                                                            1470.html ).

                                                            And don’t forget to enable the
                                                            query cache in your my.ini

                                                                                        Reply



                                                            Kovica said: March 8, 2010
                                                            at 3:29 pm
                                                            “Use Indexes” should be #1.

                                                                                        Reply


                                                                      Jean-
                                                                      Baptiste
                                                                      Jung said:
                                                                      March 9,
                                                                      2010 at
                                                                      4:20 am
                                                                      Tips        are
                                                                      displayed
                                                                      without
                                                                      importance
                                                                      order, but I
                                                                      agree      that
                                                                      indexes are
                                                                      definitely
                                                                      one of the
                                                                      best way to
                                                                      optimize
                                                                      your DB!

                                                                             Reply




http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
10 sql tips to speed up your database | CatsWhoCode.com




http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
10 sql tips to speed up your database | CatsWhoCode.com




http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
10 sql tips to speed up your database | CatsWhoCode.com




http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
10 sql tips to speed up your database | CatsWhoCode.com




                                                            wxianfeng said: March 8,
                                                            2010 at 8:57 pm
                                                            very helpful~!

                                                                                   Reply



                                                            10 sql tips to speed up your
                                                            database | [codepotato]
                                                            said: March 9, 2010 at
                                                            4:00 am
                                                            [...] 10 sql tips to speed up
                                                            your database Posted March
                                                            9th, 2010 in Blog by Gareth
                                                            http://www.catswhocode.com/blo
                                                            sql-tips-to-speed-up-your-
                                                            database [...]

                                                                                   Reply



                                                            Anonymous said: March 9,
                                                            2010 at 8:13 am
                                                            One thing anyone thinking
                                                            about optimising their queries
                                                            should bear in mind is not to
                                                            do it straight away. Get your
                                                            code working and then go
                                                            back to it and improve if
                                                            necessary.

                                                            en.wikipedia.org/wiki/Program_o

                                                            Also, you missed the word


http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
10 sql tips to speed up your database | CatsWhoCode.com

                                                            ‘may’ when you mentioned
                                                            UNION. Your source states
                                                            “may run faster than”, not “will
                                                            be faster”. I’ve found that OR
                                                            is faster than UNION and a
                                                            quick google on the subject
                                                            confirms this:

                                                            mysqlperformanceblog.com/2007
                                                            vs-union-all-performance/

                                                            Your                   source,
                                                            bcarter.com/optimsql.htm was
                                                            apparently last updated in
                                                            1996 and I’d like to think
                                                            MySQL had changed quite a
                                                            bit since then.

                                                            If anyone’s looking into
                                                            speeding up their MySQL
                                                            stuff                    then
                                                            mysqlperformanceblog.com is
                                                            highly recommended, the
                                                            English isn’t always great
                                                            though and the navigation
                                                            sucks but give it a try.

                                                            To expand on what Dirk so
                                                            rightly said, Indexes are great
                                                            for tables where there will be
                                                            a lot of read (SELECT)
                                                            operations. But if you’re often
                                                            writing to a table then the
                                                            Indexes can slow it down
                                                            because of course the index
                                                            must be updated.

                                                            Could you please consider an
                                                            update Jean, which reflects
                                                            what Dirk and I have
                                                            mentioned?

                                                                                      Reply


                                                                       Jean-
                                                                       Baptiste
                                                                       Jung said:
                                                                       March 9,
                                                                       2010 at
                                                                       8:49 am
                                                                       You’re right


http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
10 sql tips to speed up your database | CatsWhoCode.com

                                                                      about     the
                                                                      bcarter
                                                                      source:      I
                                                                      didn’t
                                                                      checked the
                                                                      “last update”
                                                                      date when
                                                                      writing the
                                                                      article, sorry
                                                                      for this.

                                                                      I       rarely
                                                                      update
                                                                      posts, but I’ll
                                                                      rewrite some
                                                                      parts     this
                                                                      week,
                                                                      according to
                                                                      my       busy
                                                                      schedule.

                                                                      Thanks a lot
                                                                      for       your
                                                                      interest!

                                                                             Reply




                                                            Timothy said: March 9,
                                                            2010 at 8:47 am
                                                            Also, here are a few more
                                                            details that could help:

                                                            1) Be wise with column types
                                                            (VarChar, Blob, Clob / Long
                                                            Text, etc).     When using
                                                            something like int, varchar,
                                                            etc with a set length the SQL
                                                            server will know what to
                                                            expect     when     conducting
                                                            queries. If you use something
                                                            like Clob / Long Text, which
                                                            does not have a set limit,
                                                            then the server will not know
                                                            what to expect, so the queries
                                                            have to way of being
                                                            optimized. Basically, it means
                                                            that every query that involves
                                                            a column with such a type will
                                                            be slow. Only use these



http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
10 sql tips to speed up your database | CatsWhoCode.com

                                                            variable types if you need to.

                                                            2) Using trims or other string
                                                            modifiers will greatly slow
                                                            down your queries. The
                                                            server will have to do this on
                                                            each row. Pretty logical when
                                                            you think about it. Any time
                                                            you are modifying a string or
                                                            other variable type within a
                                                            query you are adding a great
                                                            deal of overhead. If you can,
                                                            put these kind of things on
                                                            the server script side (PHP,
                                                            Ruby, etc).

                                                            3) If you have two tables, with
                                                            1 to 1 for every (key word
                                                            there) row, then why not use
                                                            just one table? Don’t make
                                                            more tables than you need.
                                                            Just because you’ll have
                                                            more columns doesn’t mean
                                                            you will have to SELECT
                                                            them all.

                                                            4) If you can, avoid putting
                                                            certain things in a table. Like
                                                            images. I often find out that
                                                            people store certain images in
                                                            a table as BLOBs and a
                                                            column      for    the    image
                                                            mimetype. Then they query
                                                            the image and use something
                                                            like PHP to spit it out onto the
                                                            page. The only time I’ve really
                                                            seen this to be necessary is
                                                            with      CAPTCHA       images.
                                                            Where the image is stored as
                                                            a BLOB alongside a hash
                                                            representing the text in the
                                                            image. Though, you could
                                                            probably put the images in a
                                                            folder with random filenames
                                                            and have a table matching
                                                            filenames to hash values. But
                                                            that can open up some
                                                            security risks (like black-hats
                                                            creating their own look-up
                                                            table so that they can produce
                                                            scripts).



http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
10 sql tips to speed up your database | CatsWhoCode.com


                                                                                      Reply


                                                                      Jean-
                                                                      Baptiste
                                                                      Jung said:
                                                                      March 9,
                                                                      2010 at
                                                                      5:29 pm
                                                                      Thanks for
                                                                      the
                                                                      additional
                                                                      tips!

                                                                             Reply



                                                                      Daniel said:
                                                                      March 9, 2010
                                                                      at 6:25 pm
                                                                      in reply to your tip
                                                                      3:

                                                                      i           disagree.
                                                                      sometimes it can
                                                                      make sense to split
                                                                      up a table into two,
                                                                      because searching
                                                                      in very wide (lots of
                                                                      columns)        tables
                                                                      also      has           a
                                                                      performance
                                                                      impact. splitting up
                                                                      tables is        pretty
                                                                      useful if you only
                                                                      search on a couple
                                                                      of fields in this
                                                                      table,    and       the
                                                                      majority     of     the
                                                                      other fields is just
                                                                      “additional       info”
                                                                      such                  as
                                                                      timestamps,
                                                                      user_ip,             file
                                                                      locations etc. you
                                                                      never        consider
                                                                      when looking up
                                                                      records.
                                                                      this also means
                                                                      that it is easier to
                                                                      keep your “index-


http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
10 sql tips to speed up your database | CatsWhoCode.com

                                                                      table” free from
                                                                      variable length data
                                                                      types as described
                                                                      in tip 1

                                                                                       Reply




                                                            Tomasz Kowalczyk said:
                                                            March 9, 2010 at 1:53
                                                            pm
                                                            Very good list, I would also
                                                            add using ORMs such as
                                                            Doctrine / Propel [included in
                                                            frameworks such as symfony]
                                                            that are caching everything –
                                                            no need to remember that
                                                            things.

                                                                                       Reply


                                                                      Sebastiaan
                                                                      Stok said:
                                                                      March 11,
                                                                      2010 at
                                                                      1:45 pm
                                                                      NEVER
                                                                      EVER USE
                                                                      ORM!!!
                                                                      That       is
                                                                      performance
                                                                      killer number
                                                                      one.

                                                                      There are to
                                                                      many     bad
                                                                      things about
                                                                      Object
                                                                      Relation
                                                                      Mapping.
                                                                      * You can’t,
                                                                      you      just
                                                                      can’t
                                                                      optimize the
                                                                      query     (no
                                                                      realy)
                                                                      * Having an
                                                                      oject      for
                                                                      every row is
                                                                      heavy       of


http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
10 sql tips to speed up your database | CatsWhoCode.com

                                                                      memory
                                                                      usage
                                                                      *     I even
                                                                      heard about
                                                                      an option to
                                                                      buffer query
                                                                      execution, to
                                                                      do it in one
                                                                      time. That is
                                                                      not      only
                                                                      stupid. But it
                                                                      is also very
                                                                      dangerous! I
                                                                      use
                                                                      transactions
                                                                      allot     and
                                                                      using       an
                                                                      ORM
                                                                      system will
                                                                      break this.

                                                                      Some one I
                                                                      know
                                                                      decided on
                                                                      using     an
                                                                      ORM, and is
                                                                      not     very
                                                                      happy with
                                                                      it.
                                                                      But he can’t
                                                                      switch since
                                                                      that     will
                                                                      break    the
                                                                      coding.

                                                                      And
                                                                      something
                                                                      else, ORM
                                                                      will    make
                                                                      you     brake
                                                                      with almost
                                                                      very design
                                                                      principle and
                                                                      pattern there
                                                                      is.

                                                                      Just      use
                                                                      plain      old
                                                                      SQL, it is
                                                                      much better
                                                                      and flexible.



http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
10 sql tips to speed up your database | CatsWhoCode.com


                                                                             Reply




                                                            mithu said: March 9, 2010
                                                            at 4:35 pm
                                                            Life saving tricks. Thankx.

                                                                                     Reply



                                                            CoryMathews said: March
                                                            9, 2010 at 5:07 pm
                                                            “Use join instead of sub
                                                            queries”

                                                            Depends on if the sub query
                                                            is in the select from or where
                                                            statement. So this advice
                                                            while normally true for selects
                                                            is not always true for the from
                                                            or where statement. Thus is
                                                            false.

                                                            “Use UNION instead of OR”

                                                            His benchmark says that but
                                                            every db I have worked with
                                                            says otherwise..

                                                                                     Reply



                                                            James Edinburgh said:
                                                            March 9, 2010 at 6:26
                                                            pm
                                                            Thanks for this information.

                                                            I’m just getting started in the
                                                            world of SQL and databases
                                                            and this is some solid advice.
                                                            I’ve been playing around with
                                                            WordPress and other PHP
                                                            based CMS systems and I
                                                            really really want to get
                                                            started and build my own
                                                            system, i’ll be sure to put
                                                            these tips to work when I do.

                                                                                     Reply



http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
10 sql tips to speed up your database | CatsWhoCode.com




                                                            Mes favoris du 8-03-10 au
                                                            10-03-10 » Gilles Toubiana
                                                            said: March 10, 2010 at
                                                            9:03 am
                                                            [...] 10 sql tips to speed up
                                                            your database [...]

                                                                                    Reply



                                                            » ce que j’ai vue depuis
                                                            mon retour ! oxynel, blog
                                                            communication - Agence de
                                                            communication Montpellier
                                                            said: March 10, 2010 at
                                                            11:03 am
                                                            [...] Du SQL, du SQL ou l’art
                                                            d’optimiser ses requêtes [...]

                                                                                    Reply



                                                            10 Astuces SQL pour
                                                            améliorer vos requêtes |
                                                            KubX said: March 11,
                                                            2010 at 7:51 am
                                                            [...] 10 sql tips to speed up
                                                            your database. Tags:sql [...]

                                                                                    Reply



                                                            Harsh Athalye said: March
                                                            11, 2010 at 11:38 am
                                                            Good SQL tips. Optimizing
                                                            queries is such a huge topic
                                                            in itself, that a book can be
                                                            written around it. But basically
                                                            tips mentioned here are good
                                                            enough. I would add some
                                                            more:

                                                            1. Choose wise columns
                                                            when       creating   indexes.
                                                            Creating indexes on data
                                                            types of char, varchar or such
                                                            other sizable data types can
                                                            kill the performance
                                                            2. If you have indexes on



http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
10 sql tips to speed up your database | CatsWhoCode.com

                                                            columns,     try   to     avoid
                                                            wrapping column inside a
                                                            function
                                                            3. Convert IN clause to joins
                                                            4. Set based queries definitely
                                                            beat procedural approach like
                                                            cursors     in    terms       of
                                                            performance

                                                                                     Reply



                                                            Brian said: March 12, 2010
                                                            at 2:03 am
                                                            Great tips there, I’m suprised
                                                            you’ve not mentioned the
                                                            mysql query cache, qcache.
                                                            This can dramatically speed
                                                            up mysql especially on high
                                                            traffic sites.

                                                            I must admit i’d not given
                                                            much thought to design, I
                                                            lumped all the fields in one
                                                            table which was fine when
                                                            the site had 100 entries but
                                                            now it has over 3000 things
                                                            are really starting to suffer
                                                            and I’m having to strip out the
                                                            database and organise it.

                                                            It goes to show that a little bit
                                                            of haste can really turn into a
                                                            lot of waste later on.

                                                                                     Reply



                                                            LISA TORRES said: March
                                                            13, 2010 at 10:18 am
                                                            The server will have to do
                                                            this on each row. Pretty
                                                            logical when you think about
                                                            it. Any time you are modifying
                                                            a string or other variable type
                                                            within a query you are adding
                                                            a great deal of overhead.

                                                                                     Reply




http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
10 sql tips to speed up your database | CatsWhoCode.com

                                                            quicoto said: March 17,
                                                            2010 at 11:03 am
                                                            Nice I never though how bad
                                                            can be ask for “SELECT *”
                                                            without no LIMIT or without
                                                            asking for certain fields.

                                                            Regards

                                                                                     Reply



                                                            TDAH said: March 19,
                                                            2010 at 1:25 am
                                                            Man, I just tried these on my
                                                            db and they worked great,
                                                            thanks

                                                                                     Reply



                                                            Christian said: March 19,
                                                            2010 at 5:02 am
                                                            Wow, I did not know the
                                                            update query with the nice
                                                            “CASE” syntax until now.
                                                            Very helpful. Thanks.

                                                                                     Reply



                                                            Jonathan Good said: March
                                                            20, 2010 at 1:50 pm
                                                            Great article thanks for
                                                            sharing – we can all use
                                                            faster dbs!

                                                                                     Reply



                                                            Agnideb Mishra said:
                                                            March 20, 2010 at 10:32
                                                            pm
                                                            excellent article! The queries
                                                            are particularly very helpful for
                                                            a newbie like me. Thanks for
                                                            the awesome information.

                                                                                     Reply




http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
10 sql tips to speed up your database | CatsWhoCode.com


                                                            Jack colt said: March 22,
                                                            2010 at 11:56 pm
                                                            Thanks for the tips. Most of
                                                            my websites are really slow in
                                                            loading. I tried using WP
                                                            super chache without much
                                                            success. It appears now that
                                                            the culprit lies in my design
                                                            and database.

                                                                                      Reply



                                                            Shukrit Verman said: March
                                                            27, 2010 at 11:15 pm
                                                            Thanks a bunch for this
                                                            article. I will try to fix my slow
                                                            blog using these tips

                                                                                      Reply



                                                            Chimpu Sharma said:
                                                            March 27, 2010 at 11:22
                                                            pm
                                                            I have always had to take my
                                                            web host’s help for running
                                                            sql queries. Thanks to this
                                                            article, I guess I would no
                                                            longer have to! Thanks again



                                                                                      Reply



                                                            Bob said: March 27, 2010
                                                            at 11:36 pm
                                                            “I tried using WP super cache
                                                            without much success.”

                                                            It is not working for me either.
                                                            Actually, while it is supposed
                                                            to speed up my blog, it is
                                                            actually slowing the blog
                                                            down     I would try using the
                                                            database optimization tips laid
                                                            out in this article.

                                                                                      Reply




http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
10 sql tips to speed up your database | CatsWhoCode.com



                                                            Vinith Shukla said: March
                                                            27, 2010 at 11:39 pm
                                                            Thank you very much for
                                                            these details; but for them, I
                                                            would not have known how to
                                                            optimize my SQL database!

                                                                                   Reply



                                                            Dan - LogoFoo.com said:
                                                            March 31, 2010 at 8:41
                                                            pm
                                                            Very helpful. By the way, I
                                                            think using SELECT * is not
                                                            really that harmful if you
                                                            combine it with the LIMIT
                                                            operator.

                                                                                   Reply



                                                            Blogger said: April 1, 2010
                                                            at 9:10 am
                                                            Hu, i had never thought that
                                                            those changes could speed
                                                            up my DB.
                                                            Thanks for collecting these
                                                            informations!

                                                                                   Reply



                                                            SQLを早くする10個の項目 |
                                                            アイビースター said: April 7,
                                                            2010 at 2:02 am
                                                            [...] 10 sql tips to speed up
                                                            your database [...]

                                                                                   Reply



                                                            fireRoxy said: April 13,
                                                            2010 at 4:45 am
                                                            very useful tips. thank you
                                                            very much.

                                                                                   Reply




http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
10 sql tips to speed up your database | CatsWhoCode.com


                                                            Savita Bisht said: April 22,
                                                            2010 at 11:20 pm
                                                            yeah, it is a nice article,
                                                            worked great for me.

                                                                                     Reply



                                                            Mundu said: April 29, 2010
                                                            at 11:06 am
                                                            Thanks so much for this
                                                            informative article. I now know
                                                            how to fix my extremely slow
                                                            blog

                                                                                     Reply



                                                            8 consigli e trucchi sql per
                                                            ottimizzare e rendere
                                                            performante un Database |
                                                            Pecciola said: April 30,
                                                            2010 at 12:33 am
                                                            [...] Fonte : Catswhocode [...]

                                                                                     Reply



                                                            Edward said: May 13, 2010
                                                            at 10:59 am
                                                            This post is very usefull,
                                                            thank you
                                                            I also heard about something
                                                            called stored queries so you
                                                            just call it by its name when
                                                            you do the query for example
                                                            “getnews()”     but    I’m  a
                                                            complete newbie !

                                                                                     Reply



                                                            Michael said: May 13, 2010
                                                            at 11:37 am
                                                            Thanks for         sharing this
                                                            information, it is very useful to
                                                            my visual basic programming.

                                                            Michael




http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
10 sql tips to speed up your database | CatsWhoCode.com

                                                                                   Reply



                                                            Jamie said: May 25, 2010
                                                            at 10:51 am
                                                            This is indeed very helpful. I
                                                            appreciate the fact that you
                                                            are providing screenshots so
                                                            we would know we are on the
                                                            same track while following
                                                            your tutorial. Thanks so much
                                                            for sharing.

                                                                                   Reply



                                                            Johnson said: May 31,
                                                            2010 at 7:04 pm
                                                            Great     tips,   i’ve    been
                                                            wondering if i could customise
                                                            my wordpress database, this
                                                            is    too   slow    as posts
                                                            accurelates, anyone have
                                                            interests ?

                                                                                   Reply



                                                            Jon said: June 11, 2010
                                                            at 1:05 pm
                                                            I may have to come back to
                                                            this page very shortly…a lot
                                                            of good info here.

                                                                                   Reply



                                                            Jennifer R said: June 12,
                                                            2010 at 10:34 am
                                                            Could use DB cache reload
                                                            can increase the performance
                                                            of mySQL database?

                                                                                   Reply



                                                            Büyü said: June 18, 2010
                                                            at 10:12 am
                                                            Could use DB cache reload
                                                            can increase the performance



http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
10 sql tips to speed up your database | CatsWhoCode.com

                                                            of mySQL database?

                                                                                      Reply



                                                            Alex Wolf said: June 20,
                                                            2010 at 11:16 am
                                                            Thanks for the limit tip, I
                                                            didn’t know about it.

                                                                                      Reply



                                                            Charlie said: August 3,
                                                            2010 at 1:52 pm
                                                            All good tips, particularly on
                                                            the use of UNION over OR.

                                                            As far as I know, in most
                                                            SQL databases, some uses of
                                                            OR prevent the use of an
                                                            index. So for large tables, it is
                                                            faster to scan the indices
                                                            multiple times rather than
                                                            searching the entire table
                                                            once.

                                                                                      Reply



                                                            Sumanta Sinha said:
                                                            August 20, 2010 at 5:09
                                                            am
                                                            Lately one of my PHP scripts
                                                            is making way too many sql
                                                            queries while it could easily
                                                            do the job by making fewer
                                                            queries. The reason: the
                                                            script developer did not
                                                            bother to optimize the scripts
                                                            so it could use the least
                                                            possible resource. He has
                                                            also stopped supporting the
                                                            project a year ago. Thanks to
                                                            my web host, I would have
                                                            never had known about this. I
                                                            would try your tips and see if
                                                            it helps at all. thanks

                                                                                      Reply



http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
10 sql tips to speed up your database | CatsWhoCode.com



                                                            Felipe said: November 4,
                                                            2010 at 6:47 pm
                                                            Always refreshing to see
                                                            someone pop open the hood
                                                            and show how it is done in
                                                            the age of “just-use-my-
                                                            cuttie-pretty-web-2.0-
                                                            framework-and-it-will-take-
                                                            care-of-everything-for-you-in-
                                                            a-pinch”. Congrats.

                                                                                    Reply



                                                            Shawn said: June 21, 2012
                                                            at 11:49 am
                                                            I know it’s a bit of a late post
                                                            but…..”Avoid       queries    in
                                                            loops,” while i agree with you
                                                            that you shouldn’t keep
                                                            sending SQL to a database
                                                            while looping, i think your
                                                            example is *way* to simple to
                                                            be of use. You are essentially
                                                            hard coding the case values
                                                            which 1. Shouldn’t be hard
                                                            coded       and      2.     only
                                                            manageable for a very small
                                                            set. I believe a better
                                                            alternative is to go ahead and
                                                            use the looping statement and
                                                            GENERATE the SQL needed.
                                                            Ex. Update value 1; Update
                                                            value 2; update value 3,
                                                            update value N. That is, to
                                                            aggregate the SQL into a
                                                            string variable, AND THEN
                                                            send it to the database only
                                                            once and after it is generated.

                                                                                    Reply




                                               Leave a Reply
                                               Your email address will not be published.
                                               Required fields are marked *

                                               Name *



http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
10 sql tips to speed up your database | CatsWhoCode.com




                                               Email *




                                               Website




                                               Comment




                                               Please respect the following rules:
                                               No advertising, no spam, no keyword in
                                               name field. Thank you!




                                                          18600+                  14800
                                                          RSS READERS             FOLLOWERS




http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
10 sql tips to speed up your database | CatsWhoCode.com




http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
10 sql tips to speed up your database | CatsWhoCode.com




                                               Like CWC on Facebook!

                                                    Login


                                                                             Cats Who Code on
                                                                             Facebook

                                                                                 Like              You lik
                                                                                                   Page
                                                                                        Confirm
                                                                                                   You lik
                                                                                                   Page

                                                                1,075 people like Cats Who Code.1,
                                                                people like Cats Who Code.




                                                                                Phil       Ahmed
                                                    Facebook social plugin




                                               Latest posts
                                               jQuery plugins for awesome web
                                               typography
                                               Awesome CSS3 generators to
                                               simplify front end development
                                               Practical tips to save bandwidth and
                                               reduce server load
                                               10 useful typography tips to improve
                                               your website readability
                                               WordPress dashboard hacks for
                                               developers and freelancers




http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
10 sql tips to speed up your database | CatsWhoCode.com




                                               CatsWhoCode.com is hosted by
                                               VidaHost. Use our exclusive coupon
                                               CATSWHOCODE to get a 10% discount
                                               on hosting!



                                               Search


                                                                                    Search



                                                  Latest snippets
                                               Get current url with PHP
                                               Automatically creates variables with
                                               the same name as the key in the
                                               POST array
                                               jQuery 5 row accordion
                                               Detect double click in CSS
                                               Create a page top shadow in CSS3
                                               Display a black&white version of an
                                               image using CSS
                                               Delete directory in PHP
                                               Detecting Mozilla App Install



http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
10 sql tips to speed up your database | CatsWhoCode.com

                                               Capabilities with JavaScript
                                               Allow PHP in WordPress text
                                               widgets
                                               PHP5 recursive URL-crawler


                                               Blogroll
                                               Custom Logo Design
                                               Eglise Evangélique de Binche
                                               Free Website
                                               Photoshop tutorials
                                               Punk Band
                                               seo
                                               Top Web Host Review
                                               Visiter le Québec
                                               Visiter New York




                                               10 One-Page, Fully Responsive
                                               Web Templates - only $17!
                                               Ends: 7days22min8sec
                                                          © 2008 - 2012 CatsWhoCode.com




http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]

Mais conteúdo relacionado

Mais procurados

Optimize sql server queries with these advanced tuning techniques tech repu
Optimize sql server queries with these advanced tuning techniques   tech repuOptimize sql server queries with these advanced tuning techniques   tech repu
Optimize sql server queries with these advanced tuning techniques tech repuKaing Menglieng
 
Learn Hadoop
Learn HadoopLearn Hadoop
Learn HadoopEdureka!
 
Google App Engine, Groovy and Gaelyk presentation at the Paris JUG
Google App Engine, Groovy and Gaelyk presentation at the Paris JUGGoogle App Engine, Groovy and Gaelyk presentation at the Paris JUG
Google App Engine, Groovy and Gaelyk presentation at the Paris JUGGuillaume Laforge
 
Linux Desktop Automation
Linux Desktop AutomationLinux Desktop Automation
Linux Desktop AutomationRui Lapa
 
Ms Sql Server Black Book
Ms Sql Server Black BookMs Sql Server Black Book
Ms Sql Server Black BookLiquidHub
 
Webinar slides: The Holy Grail Webinar: Become a MySQL DBA - Database Perform...
Webinar slides: The Holy Grail Webinar: Become a MySQL DBA - Database Perform...Webinar slides: The Holy Grail Webinar: Become a MySQL DBA - Database Perform...
Webinar slides: The Holy Grail Webinar: Become a MySQL DBA - Database Perform...Severalnines
 
The care and feeding of a MySQL database
The care and feeding of a MySQL databaseThe care and feeding of a MySQL database
The care and feeding of a MySQL databaseDave Stokes
 
Aegir Cpanel for Drupal Sites
Aegir Cpanel for Drupal SitesAegir Cpanel for Drupal Sites
Aegir Cpanel for Drupal SitesDamjan Cvetan
 
12 core technologies you should learn, love, and hate to be a 'real' technocrat
12 core technologies you should learn, love, and hate to be a 'real' technocrat12 core technologies you should learn, love, and hate to be a 'real' technocrat
12 core technologies you should learn, love, and hate to be a 'real' technocratlinoj
 
Collaborate 2011– Leveraging and Enriching the Capabilities of Oracle Databas...
Collaborate 2011– Leveraging and Enriching the Capabilities of Oracle Databas...Collaborate 2011– Leveraging and Enriching the Capabilities of Oracle Databas...
Collaborate 2011– Leveraging and Enriching the Capabilities of Oracle Databas...djkucera
 
SDEC2011 Big engineer vs small entreprenuer
SDEC2011 Big engineer vs small entreprenuerSDEC2011 Big engineer vs small entreprenuer
SDEC2011 Big engineer vs small entreprenuerKorea Sdec
 

Mais procurados (14)

Optimize sql server queries with these advanced tuning techniques tech repu
Optimize sql server queries with these advanced tuning techniques   tech repuOptimize sql server queries with these advanced tuning techniques   tech repu
Optimize sql server queries with these advanced tuning techniques tech repu
 
Learn Hadoop
Learn HadoopLearn Hadoop
Learn Hadoop
 
Google App Engine, Groovy and Gaelyk presentation at the Paris JUG
Google App Engine, Groovy and Gaelyk presentation at the Paris JUGGoogle App Engine, Groovy and Gaelyk presentation at the Paris JUG
Google App Engine, Groovy and Gaelyk presentation at the Paris JUG
 
Linux Desktop Automation
Linux Desktop AutomationLinux Desktop Automation
Linux Desktop Automation
 
Ms Sql Server Black Book
Ms Sql Server Black BookMs Sql Server Black Book
Ms Sql Server Black Book
 
SQL Server
SQL ServerSQL Server
SQL Server
 
Webinar slides: The Holy Grail Webinar: Become a MySQL DBA - Database Perform...
Webinar slides: The Holy Grail Webinar: Become a MySQL DBA - Database Perform...Webinar slides: The Holy Grail Webinar: Become a MySQL DBA - Database Perform...
Webinar slides: The Holy Grail Webinar: Become a MySQL DBA - Database Perform...
 
The care and feeding of a MySQL database
The care and feeding of a MySQL databaseThe care and feeding of a MySQL database
The care and feeding of a MySQL database
 
Aegir Cpanel for Drupal Sites
Aegir Cpanel for Drupal SitesAegir Cpanel for Drupal Sites
Aegir Cpanel for Drupal Sites
 
12 core technologies you should learn, love, and hate to be a 'real' technocrat
12 core technologies you should learn, love, and hate to be a 'real' technocrat12 core technologies you should learn, love, and hate to be a 'real' technocrat
12 core technologies you should learn, love, and hate to be a 'real' technocrat
 
Oit2010 model databases
Oit2010 model databasesOit2010 model databases
Oit2010 model databases
 
Collaborate 2011– Leveraging and Enriching the Capabilities of Oracle Databas...
Collaborate 2011– Leveraging and Enriching the Capabilities of Oracle Databas...Collaborate 2011– Leveraging and Enriching the Capabilities of Oracle Databas...
Collaborate 2011– Leveraging and Enriching the Capabilities of Oracle Databas...
 
SDEC2011 Big engineer vs small entreprenuer
SDEC2011 Big engineer vs small entreprenuerSDEC2011 Big engineer vs small entreprenuer
SDEC2011 Big engineer vs small entreprenuer
 
sigmod08
sigmod08sigmod08
sigmod08
 

Destaque

See sql server graphical execution plans in action tech republic
See sql server graphical execution plans in action   tech republicSee sql server graphical execution plans in action   tech republic
See sql server graphical execution plans in action tech republicKaing Menglieng
 
Speeding up queries with semi joins and anti-joins
Speeding up queries with semi joins and anti-joinsSpeeding up queries with semi joins and anti-joins
Speeding up queries with semi joins and anti-joinsKaing Menglieng
 
Using grouping sets in sql server 2008 tech republic
Using grouping sets in sql server 2008   tech republicUsing grouping sets in sql server 2008   tech republic
Using grouping sets in sql server 2008 tech republicKaing Menglieng
 
Speed up sql server apps - visual studio magazine
Speed up sql server apps  - visual studio magazineSpeed up sql server apps  - visual studio magazine
Speed up sql server apps - visual studio magazineKaing Menglieng
 
Using hash fields in sql server tech republic
Using hash fields in sql server   tech republicUsing hash fields in sql server   tech republic
Using hash fields in sql server tech republicKaing Menglieng
 
Using object dependencies in sql server 2008 tech republic
Using object dependencies in sql server 2008   tech republicUsing object dependencies in sql server 2008   tech republic
Using object dependencies in sql server 2008 tech republicKaing Menglieng
 
Sql server – query optimization – remove bookmark lookup – remove rid lookup
Sql server – query optimization – remove bookmark lookup – remove rid lookupSql server – query optimization – remove bookmark lookup – remove rid lookup
Sql server – query optimization – remove bookmark lookup – remove rid lookupKaing Menglieng
 
Sql server common interview questions and answers page 5
Sql server   common interview questions and answers page 5Sql server   common interview questions and answers page 5
Sql server common interview questions and answers page 5Kaing Menglieng
 
103 Iokfg Bk Bite Size Welcome To Kfg 2012
103 Iokfg Bk Bite Size Welcome To Kfg 2012103 Iokfg Bk Bite Size Welcome To Kfg 2012
103 Iokfg Bk Bite Size Welcome To Kfg 2012Andrew Stotter-Brooks
 
מדריך מקוצר דיווח Pmr
מדריך מקוצר דיווח Pmr מדריך מקוצר דיווח Pmr
מדריך מקוצר דיווח Pmr iroads
 
Управление репутацией в онлайне 2012
Управление репутацией в онлайне 2012Управление репутацией в онлайне 2012
Управление репутацией в онлайне 2012Nordic Agency AB
 
Presentation2012general
Presentation2012generalPresentation2012general
Presentation2012generalKim Holdstock
 
How do i... reseed a sql server identity column tech_republic
How do i... reseed a sql server identity column    tech_republicHow do i... reseed a sql server identity column    tech_republic
How do i... reseed a sql server identity column tech_republicKaing Menglieng
 
Mpkbit
MpkbitMpkbit
Mpkbitunixx
 
Global Sustainability Jam Moscow 2012
Global Sustainability Jam Moscow 2012Global Sustainability Jam Moscow 2012
Global Sustainability Jam Moscow 2012Woxar
 
Sosok presiden yang sederhana dan kharismatik
Sosok presiden yang sederhana dan kharismatikSosok presiden yang sederhana dan kharismatik
Sosok presiden yang sederhana dan kharismatikevijuniati
 
แผ่นผ้าประชาสัมพันธ์ขนาด 3 x2 เมตร
แผ่นผ้าประชาสัมพันธ์ขนาด 3 x2 เมตรแผ่นผ้าประชาสัมพันธ์ขนาด 3 x2 เมตร
แผ่นผ้าประชาสัมพันธ์ขนาด 3 x2 เมตรmontlunla
 

Destaque (20)

See sql server graphical execution plans in action tech republic
See sql server graphical execution plans in action   tech republicSee sql server graphical execution plans in action   tech republic
See sql server graphical execution plans in action tech republic
 
Speeding up queries with semi joins and anti-joins
Speeding up queries with semi joins and anti-joinsSpeeding up queries with semi joins and anti-joins
Speeding up queries with semi joins and anti-joins
 
Speed up sql
Speed up sqlSpeed up sql
Speed up sql
 
Using grouping sets in sql server 2008 tech republic
Using grouping sets in sql server 2008   tech republicUsing grouping sets in sql server 2008   tech republic
Using grouping sets in sql server 2008 tech republic
 
Speed up sql server apps - visual studio magazine
Speed up sql server apps  - visual studio magazineSpeed up sql server apps  - visual studio magazine
Speed up sql server apps - visual studio magazine
 
Using hash fields in sql server tech republic
Using hash fields in sql server   tech republicUsing hash fields in sql server   tech republic
Using hash fields in sql server tech republic
 
Using object dependencies in sql server 2008 tech republic
Using object dependencies in sql server 2008   tech republicUsing object dependencies in sql server 2008   tech republic
Using object dependencies in sql server 2008 tech republic
 
Sql server – query optimization – remove bookmark lookup – remove rid lookup
Sql server – query optimization – remove bookmark lookup – remove rid lookupSql server – query optimization – remove bookmark lookup – remove rid lookup
Sql server – query optimization – remove bookmark lookup – remove rid lookup
 
Sql server common interview questions and answers page 5
Sql server   common interview questions and answers page 5Sql server   common interview questions and answers page 5
Sql server common interview questions and answers page 5
 
103 Iokfg Bk Bite Size Welcome To Kfg 2012
103 Iokfg Bk Bite Size Welcome To Kfg 2012103 Iokfg Bk Bite Size Welcome To Kfg 2012
103 Iokfg Bk Bite Size Welcome To Kfg 2012
 
מדריך מקוצר דיווח Pmr
מדריך מקוצר דיווח Pmr מדריך מקוצר דיווח Pmr
מדריך מקוצר דיווח Pmr
 
Управление репутацией в онлайне 2012
Управление репутацией в онлайне 2012Управление репутацией в онлайне 2012
Управление репутацией в онлайне 2012
 
Lngcode
LngcodeLngcode
Lngcode
 
Presentation2012general
Presentation2012generalPresentation2012general
Presentation2012general
 
How do i... reseed a sql server identity column tech_republic
How do i... reseed a sql server identity column    tech_republicHow do i... reseed a sql server identity column    tech_republic
How do i... reseed a sql server identity column tech_republic
 
Mpkbit
MpkbitMpkbit
Mpkbit
 
Global Sustainability Jam Moscow 2012
Global Sustainability Jam Moscow 2012Global Sustainability Jam Moscow 2012
Global Sustainability Jam Moscow 2012
 
Cv hardik chauhan
Cv hardik chauhanCv hardik chauhan
Cv hardik chauhan
 
Sosok presiden yang sederhana dan kharismatik
Sosok presiden yang sederhana dan kharismatikSosok presiden yang sederhana dan kharismatik
Sosok presiden yang sederhana dan kharismatik
 
แผ่นผ้าประชาสัมพันธ์ขนาด 3 x2 เมตร
แผ่นผ้าประชาสัมพันธ์ขนาด 3 x2 เมตรแผ่นผ้าประชาสัมพันธ์ขนาด 3 x2 เมตร
แผ่นผ้าประชาสัมพันธ์ขนาด 3 x2 เมตร
 

Semelhante a 10 SQL tips speed database CatsWhoCode

Getting Started with SQL Server Compact Edition 3.51
Getting Started with SQL Server Compact Edition 3.51Getting Started with SQL Server Compact Edition 3.51
Getting Started with SQL Server Compact Edition 3.51Mark Ginnebaugh
 
Getting Started with Sql Server Compact Edition
Getting Started with Sql Server Compact EditionGetting Started with Sql Server Compact Edition
Getting Started with Sql Server Compact EditionDonRobins
 
Sybase job interview_preparation_guide
Sybase job interview_preparation_guideSybase job interview_preparation_guide
Sybase job interview_preparation_guideNV Suresh Kumar
 
Sql And Storage Considerations For Share Point Server 2010
Sql And Storage Considerations For Share Point Server 2010Sql And Storage Considerations For Share Point Server 2010
Sql And Storage Considerations For Share Point Server 2010Mike Watson
 
Lamp Stack Optimization
Lamp Stack OptimizationLamp Stack Optimization
Lamp Stack OptimizationDave Ross
 
DBA, LEVEL III TTLM Monitoring and Administering Database.docx
DBA, LEVEL III TTLM Monitoring and Administering Database.docxDBA, LEVEL III TTLM Monitoring and Administering Database.docx
DBA, LEVEL III TTLM Monitoring and Administering Database.docxseifusisay06
 
Super Sizing Youtube with Python
Super Sizing Youtube with PythonSuper Sizing Youtube with Python
Super Sizing Youtube with Pythondidip
 
Evolutionary Database Design
Evolutionary Database DesignEvolutionary Database Design
Evolutionary Database DesignAndrei Solntsev
 
Sql Health in a SharePoint environment
Sql Health in a SharePoint environmentSql Health in a SharePoint environment
Sql Health in a SharePoint environmentEnrique Lima
 
Architecture by Accident
Architecture by AccidentArchitecture by Accident
Architecture by AccidentGleicon Moraes
 
Sql interview question part 9
Sql interview question part 9Sql interview question part 9
Sql interview question part 9kaashiv1
 
Sql interview-question-part-9
Sql interview-question-part-9Sql interview-question-part-9
Sql interview-question-part-9kaashiv1
 
Database DBMS SQL ORACLE
Database DBMS SQL ORACLEDatabase DBMS SQL ORACLE
Database DBMS SQL ORACLERahul Kunchhal
 
What is Trove, the Database as a Service on OpenStack?
What is Trove, the Database as a Service on OpenStack?What is Trove, the Database as a Service on OpenStack?
What is Trove, the Database as a Service on OpenStack?OpenStack_Online
 

Semelhante a 10 SQL tips speed database CatsWhoCode (20)

SQL Saturday San Diego
SQL Saturday San DiegoSQL Saturday San Diego
SQL Saturday San Diego
 
Getting Started with SQL Server Compact Edition 3.51
Getting Started with SQL Server Compact Edition 3.51Getting Started with SQL Server Compact Edition 3.51
Getting Started with SQL Server Compact Edition 3.51
 
Getting Started with Sql Server Compact Edition
Getting Started with Sql Server Compact EditionGetting Started with Sql Server Compact Edition
Getting Started with Sql Server Compact Edition
 
Sybase job interview_preparation_guide
Sybase job interview_preparation_guideSybase job interview_preparation_guide
Sybase job interview_preparation_guide
 
Sql And Storage Considerations For Share Point Server 2010
Sql And Storage Considerations For Share Point Server 2010Sql And Storage Considerations For Share Point Server 2010
Sql And Storage Considerations For Share Point Server 2010
 
Lamp Stack Optimization
Lamp Stack OptimizationLamp Stack Optimization
Lamp Stack Optimization
 
Presentation day1oracle 12c
Presentation day1oracle 12cPresentation day1oracle 12c
Presentation day1oracle 12c
 
DBA, LEVEL III TTLM Monitoring and Administering Database.docx
DBA, LEVEL III TTLM Monitoring and Administering Database.docxDBA, LEVEL III TTLM Monitoring and Administering Database.docx
DBA, LEVEL III TTLM Monitoring and Administering Database.docx
 
Os Solomon
Os SolomonOs Solomon
Os Solomon
 
Super Sizing Youtube with Python
Super Sizing Youtube with PythonSuper Sizing Youtube with Python
Super Sizing Youtube with Python
 
Evolutionary Database Design
Evolutionary Database DesignEvolutionary Database Design
Evolutionary Database Design
 
Sql Health in a SharePoint environment
Sql Health in a SharePoint environmentSql Health in a SharePoint environment
Sql Health in a SharePoint environment
 
Architecture by Accident
Architecture by AccidentArchitecture by Accident
Architecture by Accident
 
Ebook9
Ebook9Ebook9
Ebook9
 
Sql interview question part 9
Sql interview question part 9Sql interview question part 9
Sql interview question part 9
 
Sql interview-question-part-9
Sql interview-question-part-9Sql interview-question-part-9
Sql interview-question-part-9
 
Ebook9
Ebook9Ebook9
Ebook9
 
Database DBMS SQL ORACLE
Database DBMS SQL ORACLEDatabase DBMS SQL ORACLE
Database DBMS SQL ORACLE
 
No sql exploration keyvaluestore
No sql exploration   keyvaluestoreNo sql exploration   keyvaluestore
No sql exploration keyvaluestore
 
What is Trove, the Database as a Service on OpenStack?
What is Trove, the Database as a Service on OpenStack?What is Trove, the Database as a Service on OpenStack?
What is Trove, the Database as a Service on OpenStack?
 

Mais de Kaing Menglieng

Using sql server 2008's merge statement tech republic
Using sql server 2008's merge statement   tech republicUsing sql server 2008's merge statement   tech republic
Using sql server 2008's merge statement tech republicKaing Menglieng
 
Understand when to use user defined functions in sql server tech-republic
Understand when to use user defined functions in sql server   tech-republicUnderstand when to use user defined functions in sql server   tech-republic
Understand when to use user defined functions in sql server tech-republicKaing Menglieng
 
Sql server indexed views speed up your select queries part 1 - code-projec
Sql server indexed views   speed up your select queries  part 1 - code-projecSql server indexed views   speed up your select queries  part 1 - code-projec
Sql server indexed views speed up your select queries part 1 - code-projecKaing Menglieng
 
Sql server common interview questions and answers
Sql server   common interview questions and answersSql server   common interview questions and answers
Sql server common interview questions and answersKaing Menglieng
 
Sql server common interview questions and answers page 6
Sql server   common interview questions and answers page 6Sql server   common interview questions and answers page 6
Sql server common interview questions and answers page 6Kaing Menglieng
 
Sql server common interview questions and answers page 4
Sql server   common interview questions and answers page 4Sql server   common interview questions and answers page 4
Sql server common interview questions and answers page 4Kaing Menglieng
 
Sql server common interview questions and answers page 2
Sql server   common interview questions and answers page 2Sql server   common interview questions and answers page 2
Sql server common interview questions and answers page 2Kaing Menglieng
 
Sql server – 2008 – hardware and software requirements for installing sql se
Sql server – 2008 – hardware and software requirements for installing sql seSql server – 2008 – hardware and software requirements for installing sql se
Sql server – 2008 – hardware and software requirements for installing sql seKaing Menglieng
 
Reviewing sql server permissions tech republic
Reviewing sql server permissions   tech republicReviewing sql server permissions   tech republic
Reviewing sql server permissions tech republicKaing Menglieng
 
Query optimization how to search millions of record in sql table faster -
Query optimization   how to search millions of record in sql table faster  -Query optimization   how to search millions of record in sql table faster  -
Query optimization how to search millions of record in sql table faster -Kaing Menglieng
 
New date datatypes in sql server 2008 tech republic
New date datatypes in sql server 2008   tech republicNew date datatypes in sql server 2008   tech republic
New date datatypes in sql server 2008 tech republicKaing Menglieng
 
Introduction to policy based management in sql server 2008 tech-republic
Introduction to policy based management in sql server 2008   tech-republicIntroduction to policy based management in sql server 2008   tech-republic
Introduction to policy based management in sql server 2008 tech-republicKaing Menglieng
 
Introduction to change data capture in sql server 2008 tech republic
Introduction to change data capture in sql server 2008   tech republicIntroduction to change data capture in sql server 2008   tech republic
Introduction to change data capture in sql server 2008 tech republicKaing Menglieng
 
How to import an excel file into sql server 2005 using integration services
How to import an excel file into sql server 2005 using integration services How to import an excel file into sql server 2005 using integration services
How to import an excel file into sql server 2005 using integration services Kaing Menglieng
 
How do i... query foreign data using sql server's linked servers tech_repu
How do i... query foreign data using sql server's linked servers    tech_repuHow do i... query foreign data using sql server's linked servers    tech_repu
How do i... query foreign data using sql server's linked servers tech_repuKaing Menglieng
 
Help! my sql server log file is too big!!! tech republic
Help! my sql server log file is too big!!!   tech republicHelp! my sql server log file is too big!!!   tech republic
Help! my sql server log file is too big!!! tech republicKaing Menglieng
 
Get database properties using power shell in sql server 2008 techrepublic
Get database properties using power shell in sql server 2008   techrepublicGet database properties using power shell in sql server 2008   techrepublic
Get database properties using power shell in sql server 2008 techrepublicKaing Menglieng
 
Generate a report using crystal reports in visual studio 2010 code project
Generate a report using crystal reports in visual studio 2010   code projectGenerate a report using crystal reports in visual studio 2010   code project
Generate a report using crystal reports in visual studio 2010 code projectKaing Menglieng
 

Mais de Kaing Menglieng (18)

Using sql server 2008's merge statement tech republic
Using sql server 2008's merge statement   tech republicUsing sql server 2008's merge statement   tech republic
Using sql server 2008's merge statement tech republic
 
Understand when to use user defined functions in sql server tech-republic
Understand when to use user defined functions in sql server   tech-republicUnderstand when to use user defined functions in sql server   tech-republic
Understand when to use user defined functions in sql server tech-republic
 
Sql server indexed views speed up your select queries part 1 - code-projec
Sql server indexed views   speed up your select queries  part 1 - code-projecSql server indexed views   speed up your select queries  part 1 - code-projec
Sql server indexed views speed up your select queries part 1 - code-projec
 
Sql server common interview questions and answers
Sql server   common interview questions and answersSql server   common interview questions and answers
Sql server common interview questions and answers
 
Sql server common interview questions and answers page 6
Sql server   common interview questions and answers page 6Sql server   common interview questions and answers page 6
Sql server common interview questions and answers page 6
 
Sql server common interview questions and answers page 4
Sql server   common interview questions and answers page 4Sql server   common interview questions and answers page 4
Sql server common interview questions and answers page 4
 
Sql server common interview questions and answers page 2
Sql server   common interview questions and answers page 2Sql server   common interview questions and answers page 2
Sql server common interview questions and answers page 2
 
Sql server – 2008 – hardware and software requirements for installing sql se
Sql server – 2008 – hardware and software requirements for installing sql seSql server – 2008 – hardware and software requirements for installing sql se
Sql server – 2008 – hardware and software requirements for installing sql se
 
Reviewing sql server permissions tech republic
Reviewing sql server permissions   tech republicReviewing sql server permissions   tech republic
Reviewing sql server permissions tech republic
 
Query optimization how to search millions of record in sql table faster -
Query optimization   how to search millions of record in sql table faster  -Query optimization   how to search millions of record in sql table faster  -
Query optimization how to search millions of record in sql table faster -
 
New date datatypes in sql server 2008 tech republic
New date datatypes in sql server 2008   tech republicNew date datatypes in sql server 2008   tech republic
New date datatypes in sql server 2008 tech republic
 
Introduction to policy based management in sql server 2008 tech-republic
Introduction to policy based management in sql server 2008   tech-republicIntroduction to policy based management in sql server 2008   tech-republic
Introduction to policy based management in sql server 2008 tech-republic
 
Introduction to change data capture in sql server 2008 tech republic
Introduction to change data capture in sql server 2008   tech republicIntroduction to change data capture in sql server 2008   tech republic
Introduction to change data capture in sql server 2008 tech republic
 
How to import an excel file into sql server 2005 using integration services
How to import an excel file into sql server 2005 using integration services How to import an excel file into sql server 2005 using integration services
How to import an excel file into sql server 2005 using integration services
 
How do i... query foreign data using sql server's linked servers tech_repu
How do i... query foreign data using sql server's linked servers    tech_repuHow do i... query foreign data using sql server's linked servers    tech_repu
How do i... query foreign data using sql server's linked servers tech_repu
 
Help! my sql server log file is too big!!! tech republic
Help! my sql server log file is too big!!!   tech republicHelp! my sql server log file is too big!!!   tech republic
Help! my sql server log file is too big!!! tech republic
 
Get database properties using power shell in sql server 2008 techrepublic
Get database properties using power shell in sql server 2008   techrepublicGet database properties using power shell in sql server 2008   techrepublic
Get database properties using power shell in sql server 2008 techrepublic
 
Generate a report using crystal reports in visual studio 2010 code project
Generate a report using crystal reports in visual studio 2010   code projectGenerate a report using crystal reports in visual studio 2010   code project
Generate a report using crystal reports in visual studio 2010 code project
 

10 SQL tips speed database CatsWhoCode

  • 1. 10 sql tips to speed up your database | CatsWhoCode.com 10 sql tips to speed up your database On most websites, content is stored in a database and served to visitors upon request. Databases are very fast, but there’s lots of things that you can do to enhance its speed and make sure you won’t waste any server resources. In this article, I have compiled 10 very useful tips to optimize and speed up your website database. Design your database with caution This first tip may seems obvious, but the fact is that most database problems come from badly-designed table structure. For example, I have seen people storing information such as client info and payment info in the same database column. For both the database system and developers who will have to work on it, this is not a good thing. When creating a database, always put information on various tables, use clear naming standards and make use of primary keys. Source: http://www.simple- talk.com/sql/database- administration/ten-common- database-design-mistakes/ Know what you should optimize If you want to optimize a specific query, it is extremely useful to be able to get an in- depth look at the result of a query. Using the EXPLAIN statement, you will get lots of useful info on the result produced by a specific query, as shown in the example http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
  • 2. 10 sql tips to speed up your database | CatsWhoCode.com below: EXPLAIN SELECT * FROM ref_table,other_table WHERE ref_table.key_column=other_table.co lumn; Source: http://dev.mysql.com/doc/refman/5.0/e explain.html The fastest query… Is the one you don’t send Each time you’re sending a query to the database, you’re using a bit of your server resources. This is why, on high traffic sites, the best thing you can do in order to speed up your database is to cache queries. There’s lots of solutions to implement a query cache on your server. Here are a few: AdoDB: AdoDB is a database abstraction library for PHP. It allows you to use the database system of your choice (MySQL, PostGreSQL, Interbase, and way much more) and it is designed for speed. AdoDB provides a simple, yet powerful caching system. And last but not least, AdoDB is licenced under the BSD, which means that you can use freely on your projects. A LGPL licence is also available for commercial projects. Memcached: Memcached is a distributed memory caching system which is often used to speed up dynamic database-driven websites by alleviating database load. CSQL Cache: CSQL Cache is an open-source data caching infrastructure. Never tested it personally, but it seems to be a great tool. http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
  • 3. 10 sql tips to speed up your database | CatsWhoCode.com Don’t select what you don’t need A very common way to get the desired data is to use the * symbol, which will get all fields from the desired table: SELECT * FROM wp_posts; Instead, you should definitely select only the desired fields as shown in the example below. On a very small site with, let’s say, one visitor per minute, that wouldn’t make a difference. But on a site such as Cats Who Code, it saves a lot of work for the database. SELECT title, excerpt, author FROM wp_posts; Use LIMIT It’s very common that you need to get only a specific number of records from your database. For example, a blog which is showing ten entries per page. In that case, you should definitely use the LIMIT parameter, which only selects the desired number of records. Without LIMIT, if your table has 100,000 different records, you’ll extract them all, which is unnecessary work for your server. SELECT title, excerpt, author FROM wp_posts LIMIT 10; Avoid queries in loops When using SQL along with a programming language such as PHP, it can be tempting to use SQL queries inside a loop. But doing so http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
  • 4. 10 sql tips to speed up your database | CatsWhoCode.com is like hammering your database with queries. This example illustrates the whole “queries in loops” problem: foreach ($display_order as $id => $ordinal) { $sql = "UPDATE categories SET display_order = $ordinal WHERE id = $id"; mysql_query($sql); } Here is what you should do instead: UPDATE categories SET display_order = CASE id WHEN 1 THEN 3 WHEN 2 THEN 4 WHEN 3 THEN 5 END WHERE id IN (1,2,3) Source: http://www.karlrixon.co.uk/articles/sq multiple-rows-with-different-values- and-a-single-sql-query/ Use join instead of subqueries As a programmer, subqueries are something that you can be tempted to use and abuse. Subqueries, as show below, can be very useful: SELECT a.id, (SELECT MAX(created) FROM posts WHERE author_id = a.id) AS latest_post FROM authors a Although subqueries are useful, they often can be replaced by a join, which is definitely http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
  • 5. 10 sql tips to speed up your database | CatsWhoCode.com faster to execute. SELECT a.id, MAX(p.created) AS latest_post FROM authors a INNER JOIN posts p ON (a.id = p.author_id) GROUP BY a.id Source: http://20bits.com/articles/10- tips-for-optimizing-mysql-queries- that-dont-suck/ Be careful when using wildcards Wildcards are very useful because they can substitute for one or more characters when searching for data in a database. I’m not saying that you shouldn’t use them, but instead, you should use them with caution and not use the full wildcard when the prefix or postfix wildcard can do the same job. In fact, doing a full wildcard search on a million records will certainly kill your database. #Full wildcard SELECT * FROM TABLE WHERE COLUMN LIKE '%hello%'; #Postfix wildcard SELECT * FROM TABLE WHERE COLUMN LIKE 'hello%'; #Prefix wildcard SELECT * FROM TABLE WHERE COLUMN LIKE '%hello'; Source: http://hungred.com/useful- information/ways-optimize-sql- queries/ Use UNION instead of OR The following example use the OR statement to get the result: http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
  • 6. 10 sql tips to speed up your database | CatsWhoCode.com SELECT * FROM a, b WHERE a.p = b.q or a.x = b.y; The UNION statement allows you to combine the result sets of 2 or more select queries. The following example will return the same result that the above query gets, but it will be faster: SELECT * FROM a, b WHERE a.p = b.q UNION SELECT * FROM a, b WHERE a.x = b.y Source: http://www.bcarter.com/optimsql.htm Use indexes Database indexes are similar to those you can find in libraries: They allow the database to find the requested information faster, just like a library index will allow a reader to find what they’re looking for without loosing time. An Index can be created on a single column or a combination of columns in a database table. A table index is a database structure that arranges the values of one or more columns in a database table in specific order. The following query will create an index on the Model column from the Product table. The index is called idxModel: CREATE INDEX idxModel ON Product (Model); Source: http://www.sql- tutorial.com/sql-indexes-sql-tutorial/ More posts about SQL http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
  • 7. 10 sql tips to speed up your database | CatsWhoCode.com Getting started with CouchDB: a beginner’s gu Share this article like http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database AVqRj-yi 387 49 20 tweets 1 retweet Like Like Comments (54) - Leave yours Bart Jacobs said: March 8, 2010 at 11:49 am This article is very useful for anyone working with mySQL (and with databases in general). For high traffic websites especially, database queries can be the bottleneck (and often are). Thanks for this great post! Reply Can Aydoğan said: March 8, 2010 at 1:27 pm Great article. Thanks for sharing! Reply http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
  • 8. 10 sql tips to speed up your database | CatsWhoCode.com Metric Stormtrooper said: March 8, 2010 at 3:27 pm your table type can also make a lot of difference. MyISAM is better if you expect a lot of reads, InnoDB if you need more writes in a table (see also http://www.softwareprojects.com mysql-storage-engines- 1470.html ). And don’t forget to enable the query cache in your my.ini Reply Kovica said: March 8, 2010 at 3:29 pm “Use Indexes” should be #1. Reply Jean- Baptiste Jung said: March 9, 2010 at 4:20 am Tips are displayed without importance order, but I agree that indexes are definitely one of the best way to optimize your DB! Reply http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
  • 9. 10 sql tips to speed up your database | CatsWhoCode.com http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
  • 10. 10 sql tips to speed up your database | CatsWhoCode.com http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
  • 11. 10 sql tips to speed up your database | CatsWhoCode.com http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
  • 12. 10 sql tips to speed up your database | CatsWhoCode.com wxianfeng said: March 8, 2010 at 8:57 pm very helpful~! Reply 10 sql tips to speed up your database | [codepotato] said: March 9, 2010 at 4:00 am [...] 10 sql tips to speed up your database Posted March 9th, 2010 in Blog by Gareth http://www.catswhocode.com/blo sql-tips-to-speed-up-your- database [...] Reply Anonymous said: March 9, 2010 at 8:13 am One thing anyone thinking about optimising their queries should bear in mind is not to do it straight away. Get your code working and then go back to it and improve if necessary. en.wikipedia.org/wiki/Program_o Also, you missed the word http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
  • 13. 10 sql tips to speed up your database | CatsWhoCode.com ‘may’ when you mentioned UNION. Your source states “may run faster than”, not “will be faster”. I’ve found that OR is faster than UNION and a quick google on the subject confirms this: mysqlperformanceblog.com/2007 vs-union-all-performance/ Your source, bcarter.com/optimsql.htm was apparently last updated in 1996 and I’d like to think MySQL had changed quite a bit since then. If anyone’s looking into speeding up their MySQL stuff then mysqlperformanceblog.com is highly recommended, the English isn’t always great though and the navigation sucks but give it a try. To expand on what Dirk so rightly said, Indexes are great for tables where there will be a lot of read (SELECT) operations. But if you’re often writing to a table then the Indexes can slow it down because of course the index must be updated. Could you please consider an update Jean, which reflects what Dirk and I have mentioned? Reply Jean- Baptiste Jung said: March 9, 2010 at 8:49 am You’re right http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
  • 14. 10 sql tips to speed up your database | CatsWhoCode.com about the bcarter source: I didn’t checked the “last update” date when writing the article, sorry for this. I rarely update posts, but I’ll rewrite some parts this week, according to my busy schedule. Thanks a lot for your interest! Reply Timothy said: March 9, 2010 at 8:47 am Also, here are a few more details that could help: 1) Be wise with column types (VarChar, Blob, Clob / Long Text, etc). When using something like int, varchar, etc with a set length the SQL server will know what to expect when conducting queries. If you use something like Clob / Long Text, which does not have a set limit, then the server will not know what to expect, so the queries have to way of being optimized. Basically, it means that every query that involves a column with such a type will be slow. Only use these http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
  • 15. 10 sql tips to speed up your database | CatsWhoCode.com variable types if you need to. 2) Using trims or other string modifiers will greatly slow down your queries. The server will have to do this on each row. Pretty logical when you think about it. Any time you are modifying a string or other variable type within a query you are adding a great deal of overhead. If you can, put these kind of things on the server script side (PHP, Ruby, etc). 3) If you have two tables, with 1 to 1 for every (key word there) row, then why not use just one table? Don’t make more tables than you need. Just because you’ll have more columns doesn’t mean you will have to SELECT them all. 4) If you can, avoid putting certain things in a table. Like images. I often find out that people store certain images in a table as BLOBs and a column for the image mimetype. Then they query the image and use something like PHP to spit it out onto the page. The only time I’ve really seen this to be necessary is with CAPTCHA images. Where the image is stored as a BLOB alongside a hash representing the text in the image. Though, you could probably put the images in a folder with random filenames and have a table matching filenames to hash values. But that can open up some security risks (like black-hats creating their own look-up table so that they can produce scripts). http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
  • 16. 10 sql tips to speed up your database | CatsWhoCode.com Reply Jean- Baptiste Jung said: March 9, 2010 at 5:29 pm Thanks for the additional tips! Reply Daniel said: March 9, 2010 at 6:25 pm in reply to your tip 3: i disagree. sometimes it can make sense to split up a table into two, because searching in very wide (lots of columns) tables also has a performance impact. splitting up tables is pretty useful if you only search on a couple of fields in this table, and the majority of the other fields is just “additional info” such as timestamps, user_ip, file locations etc. you never consider when looking up records. this also means that it is easier to keep your “index- http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
  • 17. 10 sql tips to speed up your database | CatsWhoCode.com table” free from variable length data types as described in tip 1 Reply Tomasz Kowalczyk said: March 9, 2010 at 1:53 pm Very good list, I would also add using ORMs such as Doctrine / Propel [included in frameworks such as symfony] that are caching everything – no need to remember that things. Reply Sebastiaan Stok said: March 11, 2010 at 1:45 pm NEVER EVER USE ORM!!! That is performance killer number one. There are to many bad things about Object Relation Mapping. * You can’t, you just can’t optimize the query (no realy) * Having an oject for every row is heavy of http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
  • 18. 10 sql tips to speed up your database | CatsWhoCode.com memory usage * I even heard about an option to buffer query execution, to do it in one time. That is not only stupid. But it is also very dangerous! I use transactions allot and using an ORM system will break this. Some one I know decided on using an ORM, and is not very happy with it. But he can’t switch since that will break the coding. And something else, ORM will make you brake with almost very design principle and pattern there is. Just use plain old SQL, it is much better and flexible. http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
  • 19. 10 sql tips to speed up your database | CatsWhoCode.com Reply mithu said: March 9, 2010 at 4:35 pm Life saving tricks. Thankx. Reply CoryMathews said: March 9, 2010 at 5:07 pm “Use join instead of sub queries” Depends on if the sub query is in the select from or where statement. So this advice while normally true for selects is not always true for the from or where statement. Thus is false. “Use UNION instead of OR” His benchmark says that but every db I have worked with says otherwise.. Reply James Edinburgh said: March 9, 2010 at 6:26 pm Thanks for this information. I’m just getting started in the world of SQL and databases and this is some solid advice. I’ve been playing around with WordPress and other PHP based CMS systems and I really really want to get started and build my own system, i’ll be sure to put these tips to work when I do. Reply http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
  • 20. 10 sql tips to speed up your database | CatsWhoCode.com Mes favoris du 8-03-10 au 10-03-10 » Gilles Toubiana said: March 10, 2010 at 9:03 am [...] 10 sql tips to speed up your database [...] Reply » ce que j’ai vue depuis mon retour ! oxynel, blog communication - Agence de communication Montpellier said: March 10, 2010 at 11:03 am [...] Du SQL, du SQL ou l’art d’optimiser ses requêtes [...] Reply 10 Astuces SQL pour améliorer vos requêtes | KubX said: March 11, 2010 at 7:51 am [...] 10 sql tips to speed up your database. Tags:sql [...] Reply Harsh Athalye said: March 11, 2010 at 11:38 am Good SQL tips. Optimizing queries is such a huge topic in itself, that a book can be written around it. But basically tips mentioned here are good enough. I would add some more: 1. Choose wise columns when creating indexes. Creating indexes on data types of char, varchar or such other sizable data types can kill the performance 2. If you have indexes on http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
  • 21. 10 sql tips to speed up your database | CatsWhoCode.com columns, try to avoid wrapping column inside a function 3. Convert IN clause to joins 4. Set based queries definitely beat procedural approach like cursors in terms of performance Reply Brian said: March 12, 2010 at 2:03 am Great tips there, I’m suprised you’ve not mentioned the mysql query cache, qcache. This can dramatically speed up mysql especially on high traffic sites. I must admit i’d not given much thought to design, I lumped all the fields in one table which was fine when the site had 100 entries but now it has over 3000 things are really starting to suffer and I’m having to strip out the database and organise it. It goes to show that a little bit of haste can really turn into a lot of waste later on. Reply LISA TORRES said: March 13, 2010 at 10:18 am The server will have to do this on each row. Pretty logical when you think about it. Any time you are modifying a string or other variable type within a query you are adding a great deal of overhead. Reply http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
  • 22. 10 sql tips to speed up your database | CatsWhoCode.com quicoto said: March 17, 2010 at 11:03 am Nice I never though how bad can be ask for “SELECT *” without no LIMIT or without asking for certain fields. Regards Reply TDAH said: March 19, 2010 at 1:25 am Man, I just tried these on my db and they worked great, thanks Reply Christian said: March 19, 2010 at 5:02 am Wow, I did not know the update query with the nice “CASE” syntax until now. Very helpful. Thanks. Reply Jonathan Good said: March 20, 2010 at 1:50 pm Great article thanks for sharing – we can all use faster dbs! Reply Agnideb Mishra said: March 20, 2010 at 10:32 pm excellent article! The queries are particularly very helpful for a newbie like me. Thanks for the awesome information. Reply http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
  • 23. 10 sql tips to speed up your database | CatsWhoCode.com Jack colt said: March 22, 2010 at 11:56 pm Thanks for the tips. Most of my websites are really slow in loading. I tried using WP super chache without much success. It appears now that the culprit lies in my design and database. Reply Shukrit Verman said: March 27, 2010 at 11:15 pm Thanks a bunch for this article. I will try to fix my slow blog using these tips Reply Chimpu Sharma said: March 27, 2010 at 11:22 pm I have always had to take my web host’s help for running sql queries. Thanks to this article, I guess I would no longer have to! Thanks again Reply Bob said: March 27, 2010 at 11:36 pm “I tried using WP super cache without much success.” It is not working for me either. Actually, while it is supposed to speed up my blog, it is actually slowing the blog down I would try using the database optimization tips laid out in this article. Reply http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
  • 24. 10 sql tips to speed up your database | CatsWhoCode.com Vinith Shukla said: March 27, 2010 at 11:39 pm Thank you very much for these details; but for them, I would not have known how to optimize my SQL database! Reply Dan - LogoFoo.com said: March 31, 2010 at 8:41 pm Very helpful. By the way, I think using SELECT * is not really that harmful if you combine it with the LIMIT operator. Reply Blogger said: April 1, 2010 at 9:10 am Hu, i had never thought that those changes could speed up my DB. Thanks for collecting these informations! Reply SQLを早くする10個の項目 | アイビースター said: April 7, 2010 at 2:02 am [...] 10 sql tips to speed up your database [...] Reply fireRoxy said: April 13, 2010 at 4:45 am very useful tips. thank you very much. Reply http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
  • 25. 10 sql tips to speed up your database | CatsWhoCode.com Savita Bisht said: April 22, 2010 at 11:20 pm yeah, it is a nice article, worked great for me. Reply Mundu said: April 29, 2010 at 11:06 am Thanks so much for this informative article. I now know how to fix my extremely slow blog Reply 8 consigli e trucchi sql per ottimizzare e rendere performante un Database | Pecciola said: April 30, 2010 at 12:33 am [...] Fonte : Catswhocode [...] Reply Edward said: May 13, 2010 at 10:59 am This post is very usefull, thank you I also heard about something called stored queries so you just call it by its name when you do the query for example “getnews()” but I’m a complete newbie ! Reply Michael said: May 13, 2010 at 11:37 am Thanks for sharing this information, it is very useful to my visual basic programming. Michael http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
  • 26. 10 sql tips to speed up your database | CatsWhoCode.com Reply Jamie said: May 25, 2010 at 10:51 am This is indeed very helpful. I appreciate the fact that you are providing screenshots so we would know we are on the same track while following your tutorial. Thanks so much for sharing. Reply Johnson said: May 31, 2010 at 7:04 pm Great tips, i’ve been wondering if i could customise my wordpress database, this is too slow as posts accurelates, anyone have interests ? Reply Jon said: June 11, 2010 at 1:05 pm I may have to come back to this page very shortly…a lot of good info here. Reply Jennifer R said: June 12, 2010 at 10:34 am Could use DB cache reload can increase the performance of mySQL database? Reply Büyü said: June 18, 2010 at 10:12 am Could use DB cache reload can increase the performance http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
  • 27. 10 sql tips to speed up your database | CatsWhoCode.com of mySQL database? Reply Alex Wolf said: June 20, 2010 at 11:16 am Thanks for the limit tip, I didn’t know about it. Reply Charlie said: August 3, 2010 at 1:52 pm All good tips, particularly on the use of UNION over OR. As far as I know, in most SQL databases, some uses of OR prevent the use of an index. So for large tables, it is faster to scan the indices multiple times rather than searching the entire table once. Reply Sumanta Sinha said: August 20, 2010 at 5:09 am Lately one of my PHP scripts is making way too many sql queries while it could easily do the job by making fewer queries. The reason: the script developer did not bother to optimize the scripts so it could use the least possible resource. He has also stopped supporting the project a year ago. Thanks to my web host, I would have never had known about this. I would try your tips and see if it helps at all. thanks Reply http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
  • 28. 10 sql tips to speed up your database | CatsWhoCode.com Felipe said: November 4, 2010 at 6:47 pm Always refreshing to see someone pop open the hood and show how it is done in the age of “just-use-my- cuttie-pretty-web-2.0- framework-and-it-will-take- care-of-everything-for-you-in- a-pinch”. Congrats. Reply Shawn said: June 21, 2012 at 11:49 am I know it’s a bit of a late post but…..”Avoid queries in loops,” while i agree with you that you shouldn’t keep sending SQL to a database while looping, i think your example is *way* to simple to be of use. You are essentially hard coding the case values which 1. Shouldn’t be hard coded and 2. only manageable for a very small set. I believe a better alternative is to go ahead and use the looping statement and GENERATE the SQL needed. Ex. Update value 1; Update value 2; update value 3, update value N. That is, to aggregate the SQL into a string variable, AND THEN send it to the database only once and after it is generated. Reply Leave a Reply Your email address will not be published. Required fields are marked * Name * http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
  • 29. 10 sql tips to speed up your database | CatsWhoCode.com Email * Website Comment Please respect the following rules: No advertising, no spam, no keyword in name field. Thank you! 18600+ 14800 RSS READERS FOLLOWERS http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
  • 30. 10 sql tips to speed up your database | CatsWhoCode.com http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
  • 31. 10 sql tips to speed up your database | CatsWhoCode.com Like CWC on Facebook! Login Cats Who Code on Facebook Like You lik Page Confirm You lik Page 1,075 people like Cats Who Code.1, people like Cats Who Code. Phil Ahmed Facebook social plugin Latest posts jQuery plugins for awesome web typography Awesome CSS3 generators to simplify front end development Practical tips to save bandwidth and reduce server load 10 useful typography tips to improve your website readability WordPress dashboard hacks for developers and freelancers http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
  • 32. 10 sql tips to speed up your database | CatsWhoCode.com CatsWhoCode.com is hosted by VidaHost. Use our exclusive coupon CATSWHOCODE to get a 10% discount on hosting! Search Search Latest snippets Get current url with PHP Automatically creates variables with the same name as the key in the POST array jQuery 5 row accordion Detect double click in CSS Create a page top shadow in CSS3 Display a black&white version of an image using CSS Delete directory in PHP Detecting Mozilla App Install http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]
  • 33. 10 sql tips to speed up your database | CatsWhoCode.com Capabilities with JavaScript Allow PHP in WordPress text widgets PHP5 recursive URL-crawler Blogroll Custom Logo Design Eglise Evangélique de Binche Free Website Photoshop tutorials Punk Band seo Top Web Host Review Visiter le Québec Visiter New York 10 One-Page, Fully Responsive Web Templates - only $17! Ends: 7days22min8sec © 2008 - 2012 CatsWhoCode.com http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database[08/29/2012 11:38:07 AM]