Alpha Software Mobile Development Tools:   Alpha Anywhere    |   Alpha TransForm subscribe to our YouTube Channel  Follow Us on LinkedIn  Follow Us on Twitter  Follow Us on Facebook

Announcement

Collapse

The Alpha Software Forum Participation Guidelines

The Alpha Software Forum is a free forum created for Alpha Software Developer Community to ask for help, exchange ideas, and share solutions. Alpha Software strives to create an environment where all members of the community can feel safe to participate. In order to ensure the Alpha Software Forum is a place where all feel welcome, forum participants are expected to behave as follows:
  • Be professional in your conduct
  • Be kind to others
  • Be constructive when giving feedback
  • Be open to new ideas and suggestions
  • Stay on topic


Be sure all comments and threads you post are respectful. Posts that contain any of the following content will be considered a violation of your agreement as a member of the Alpha Software Forum Community and will be moderated:
  • Spam.
  • Vulgar language.
  • Quotes from private conversations without permission, including pricing and other sales related discussions.
  • Personal attacks, insults, or subtle put-downs.
  • Harassment, bullying, threatening, mocking, shaming, or deriding anyone.
  • Sexist, racist, homophobic, transphobic, ableist, or otherwise discriminatory jokes and language.
  • Sexually explicit or violent material, links, or language.
  • Pirated, hacked, or copyright-infringing material.
  • Encouraging of others to engage in the above behaviors.


If a thread or post is found to contain any of the content outlined above, a moderator may choose to take one of the following actions:
  • Remove the Post or Thread - the content is removed from the forum.
  • Place the User in Moderation - all posts and new threads must be approved by a moderator before they are posted.
  • Temporarily Ban the User - user is banned from forum for a period of time.
  • Permanently Ban the User - user is permanently banned from the forum.


Moderators may also rename posts and threads if they are too generic or do not property reflect the content.

Moderators may move threads if they have been posted in the incorrect forum.

Threads/Posts questioning specific moderator decisions or actions (such as "why was a user banned?") are not allowed and will be removed.

The owners of Alpha Software Corporation (Forum Owner) reserve the right to remove, edit, move, or close any thread for any reason; or ban any forum member without notice, reason, or explanation.

Community members are encouraged to click the "Report Post" icon in the lower left of a given post if they feel the post is in violation of the rules. This will alert the Moderators to take a look.

Alpha Software Corporation may amend the guidelines from time to time and may also vary the procedures it sets out where appropriate in a particular case. Your agreement to comply with the guidelines will be deemed agreement to any changes to it.



Bonus TIPS for Successful Posting

Try a Search First
It is highly recommended that a Search be done on your topic before posting, as many questions have been answered in prior posts. As with any search engine, the shorter the search term, the more "hits" will be returned, but the more specific the search term is, the greater the relevance of those "hits". Searching for "table" might well return every message on the board while "tablesum" would greatly restrict the number of messages returned.

When you do post
First, make sure you are posting your question in the correct forum. For example, if you post an issue regarding Desktop applications on the Mobile & Browser Applications board , not only will your question not be seen by the appropriate audience, it may also be removed or relocated.

The more detail you provide about your problem or question, the more likely someone is to understand your request and be able to help. A sample database with a minimum of records (and its support files, zipped together) will make it much easier to diagnose issues with your application. Screen shots of error messages are especially helpful.

When explaining how to reproduce your problem, please be as detailed as possible. Describe every step, click-by-click and keypress-by-keypress. Otherwise when others try to duplicate your problem, they may do something slightly different and end up with different results.

A note about attachments
You may only attach one file to each message. Attachment file size is limited to 2MB. If you need to include several files, you may do so by zipping them into a single archive.

If you forgot to attach your files to your post, please do NOT create a new thread. Instead, reply to your original message and attach the file there.

When attaching screen shots, it is best to attach an image file (.BMP, .JPG, .GIF, .PNG, etc.) or a zip file of several images, as opposed to a Word document containing the screen shots. Because Word documents are prone to viruses, many message board users will not open your Word file, therefore limiting their ability to help you.

Similarly, if you are uploading a zipped archive, you should simply create a .ZIP file and not a self-extracting .EXE as many users will not run your EXE file.
See more
See less

SQL Speed

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    SQL Speed

    I have another thread about a report "suddenly" getting very slow in MS SQL. But after spending the afternoon on this, the lesson is really more about how to get better performance from MS SQL. I figured I'd share what I've learned with a new properly titled thread for anyone searching.

    The problem was that I have a grouped report --- total count of groups by the time it's done is about 200 for any one run. Each group has lots of little lists in it -- like subreports. But what I did to get those data strings was create functions to do the job and included them as calculated fields in the report. I like this model a lot, especially now that I've solved the speed problem. To help with your understanding of this, the report is about each activity that each trainer spends with each animal each day. Each training session has it's own list of foods, it's own list of medicines, etc. So for 200 groups multiplied by 5 "sub lists" per group, I have 1,000 "sub lists". The food list for one might look like this:

    Capelin 1.25 lbs
    Clams .50 lbs
    Smelt .25 lbs

    My report, however, was taking more than 5 minutes to run. And this is a small one -- larger customers are going to have more animals, more trainers, more sessions per day and I had visions of 15 minute report generation time. Clearly, I had to do something.

    Here's the select statement I use for food, for instance:

    Code:
        SELECT Distinct Food_Names.Foodname, Session_Food.Qty as Qty
        FROM Session_Food Session_Food
             INNER JOIN (Food_Master Food_Master
                 INNER JOIN Food_Names Food_Names
                     ON  Food_Master.Description = Food_Names.ID )
                 ON  Session_Food.Food_ID = Food_Master.Description
        where Session_Food.Session_ID = :Session_ID
    Now I already know that some people are thinking I should have a View, and the use of Distinct here doesn't help, but the lesson I have is still good.

    So a typical report might run this Select statement and some surrounding code 200 times, multiplied by 5 of these.

    With a little analysis, I discovered that the average time to run this function for food was 0.3 seconds. In my sample, 70 times the function ran in under 0.27 seconds, but the other 130 all exceeded 1 second - up to 1.5 seconds. In general, not all that bad a performance, but there was a clear jump from the "fast" executions to the "slow" ones.

    My experiment to fix this worked.

    If you look at the Select statement above, you'll see some joins. Having dealt with Alpha's dbfs for a very long time, and knowing a little bit about potential sources of problems, I decided to install indexes on the child fields of the joined tables.

    Voila! For 200 executions of the Foods function, total time dropped from 65 seconds to - ready for this? : 4.2 seconds. Wow! Average time dropped from 0.3 seconds to 0.02 seconds.

    So, indexes on the child fields where a Join is created will go a very, very long way to achieving the kind of performance you need.

    Off to make a few people happier now....
    -Steve
    sigpic

    #2
    Re: SQL Speed

    Well said. Thanks for sharing a real-world example of DB optimization. Can't always blame the software, right? In fact, maybe I will go over some of my schemas right now!!

    Comment


      #3
      Re: SQL Speed

      Good point Steve, well presented.
      Glen Schild



      My Blog

      Comment


        #4
        Re: SQL Speed

        In a database that I'm working on, where we are converting from dbf to MySQL. I have been struggling with what field type to use for "characters". For the most part I am using VARCHAR. The comment below which came from the MySQL site, however may make me change a field type to CHAR in some instances, at least I have found some advantage in my limited testing where the field is indexed. Just a thought.
        mike

        The final decision is your call. Here are the pros and cons of each. The only real benefit to using CHAR is that it allows MySQL to run select statements a bit quicker since all the records are of a fixed length. I don't know how *much* quicker though. I suggest you use VARCHAR and then switch to CHAR if performance is a problem.

        VARCHAR

        - Trailing white space is preserved. (MySQL 5.0.3+ only.)
        - Minimal storage overhead. A 17 character record takes up only 17 characters of space.
        - Slower SELECTs when searching this column.

        CHAR

        - Trailing white space is always removed.
        - Every record takes up a fixed amount of space - 19 characters in your case. Your 17 character records will waste two bytes of storage.
        - Faster SELECTs when searching this column.
        Mike Reed
        Phoenix, AZ

        Comment


          #5
          Re: SQL Speed

          Originally posted by Mike Reed View Post
          In a database that I'm working on, where we are converting from dbf to MySQL. I have been struggling with what field type to use for "characters". For the most part I am using VARCHAR. The comment below which came from the MySQL site, however may make me change a field type to CHAR in some instances, at least I have found some advantage in my limited testing where the field is indexed. Just a thought.
          mike

          The final decision is your call. Here are the pros and cons of each. The only real benefit to using CHAR is that it allows MySQL to run select statements a bit quicker since all the records are of a fixed length. I don't know how *much* quicker though. I suggest you use VARCHAR and then switch to CHAR if performance is a problem.

          VARCHAR

          - Trailing white space is preserved. (MySQL 5.0.3+ only.)
          - Minimal storage overhead. A 17 character record takes up only 17 characters of space.
          - Slower SELECTs when searching this column.
          One small correction, and clarifcation an CHAR(19) field always uses 19 characters of space to store anything, a VARCHAR(19) has 1 or 2 bytes that stores the length so if a VARCHAR(19) field has 17 characters of data it actually uses 18 characters.

          Not a big difference, but a good reason why you would use CHAR(1) fields and not a VARCHAR(1) field.

          Comment


            #6
            Re: SQL Speed

            Here are some SQL optimization's I've been making. (I'm using MySQL 5.whateveristhelatest5ithink)

            1) Changing primary, auto-increment keys (and other small numeric values) to their 'lowest' that I can safely set. For Example, if I know a table is never going to go above 10 records, Why waste the space on an INT, when I can use a TINYINT (Goes from 4 bytes to 1 byte). Yes, I know, space is cheap, but it's still faster to search through 10 bytes than 40 bytes. Bonus, If it's unsigned, a TINYINT can go to 255 instead of 127. (http://stackoverflow.com/a/2991419) (using smaller INT's also reduces backup size) (also SMALLINT. I don't trust MEDIUMINT...3 bytes sounds fishy)

            2) For anyone using ulinks, CHAR(36) is faster than VARCHAR(36). Same goes for Zip Codes, CHAR(5) is faster than VARCHAR(5), (or 6 or 9, depending on the country). We can generalize it even further and say CHAR is always going to be faster than VARCHAR when the string is a FIXED LENGTH!!! That means CHAR will not be faster if you have strings of 5, 6, or 9. (See Mike's post above)

            3) Proper use of UNIQUE and Indexes!! Very important as seen in OP. I had a very similar situation where adding a few indexes cut down the query time by over 2000%!
            3a) Learn to use the sql commands EXPLAIN (EXTENDED) and ANALYSE. This will help you determine the best column type for your data (analyse) and it will also help you troubleshoot slow queries (explain). Use some common sense when you analyse the tables, as sql will give you the smallest data type, not necessarily the best. It returned quite a few ENUM's which were inappropriate in my case.

            1 and 2 are based on my own research and are not guaranteed to work for everyone, in fact, they are probably wrong and I'm just crazy.
            3 and 3a are absolutely correct as they are in all the manuals, websites, handbooks, seminars and workshops for SQL, so follow it!

            Comment


              #7
              Re: SQL Speed

              Yeah I totally agree about indexes...indexes are key to a good running database. Really, unless its a very tiny table <1000 records, any query should be hitting either a primary or secondary index. And any inner join should also be on indexes too for all of the join components and where clauses.

              The one watchout is don't have too many indexes, since each extra index slows down an insert/update/delete since SQL has to update the indexes too. If you find you are adding a ton of indexes to a table you probably need to redesign the table structure or break it out in to different tables.

              Scott

              Comment

              Working...
              X