Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Query Improvement Related to Lounge Post

Query Improvement Related to Lounge Post

Scheduled Pinned Locked Moved Database
databasetutorialcode-review
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    programmervb netc
    wrote on last edited by
    #1

    Ideas of how to improve this query

    SELECT `UniqueNumber-900` AS `ClientNumber`,
    (SELECT `AdmitDate`
    FROM `cl_clientsvcs_dayhab`
    WHERE `ClientCommonNumber` = `cl_client_common`.`UniqueNumber-900`
    ORDER BY `AdmitDate` ASC
    LIMIT 1)
    AS `InitialAdmitDate`,
    CONCAT(`cl_client_common`.`LastName`, ', ', `cl_client_common`.`FirstName`, ' ', `cl_client_common`.`MiddleName`)
    AS `ClientName`,
    `xx_component_master`.`ComponentCode`,
    `MedicaidNumber`,
    `MedicareNumber`,
    CONCAT(`cl_client_common`.`Address1`, IF(`cl_client_common`.`Address2` = '', '', CONCAT(', ', `cl_client_common`.`Address2`)))
    AS `Address1`,
    CONCAT(`cl_client_common`.`City`, ', ', `client_state`.`StateAbbrev`, ' ', `cl_client_common`.`ZipCode`)
    AS `CityStateZip`,
    CONCAT(`cl_client_common`.`PhoneNumber`, IF(`cl_client_common`.`PhoneNumber2` <> '', CONCAT('; ', `cl_client_common`.`PhoneNumber2`), ''))
    AS `ClientPhone`,
    `cl_client_common`.`PhoneNumber2` AS `ClientPhone2`,
    `cl_client_common`.`DateOfBirth`,
    `Gender`,
    `cl_client_common`.`SocialSecurityNumber`,
    `DDSNumber`,
    LEFT(`xx_marital_status`.`Description`, 1) AS `MaritalStatus`,
    LEFT(`xx_race_master`.`Description`, 1) AS `Race`,
    `xx_legal_status`.`Description` AS `LegalStatus`,
    CONCAT(`xx_sc_master`.`LastName`, ', ', `xx_sc_master`.`FirstName`) AS `ServiceCoordinator`,
    `cl_clientsvcs_dayhab`.`AdmitDate`,
    `cl_client_guardians`.`SelfGuardian`,
    `xx_county_master`.`CountyName`,
    CASE `Guardian1`.`NameFormat`
    WHEN 1 THEN
    CONCAT(`Guardian1`.`FirstName1`, ' ', `Guardian1`.`LastName1`)
    WHEN 2 THEN
    CONCAT(`Guardian1`.`FirstName1`, ' & ', `Guardian1`.`FirstName2`, ' ', `Guardian1`.`LastName1`)
    WHEN 3 THEN
    CONCAT(`Guardian1`.`FirstName1`, ' ', `Guardian1`.`LastName1`, ' & ', `Guardian1`.`FirstName2`,
    ' ', `Guardian1`.`LastName2`)
    END
    AS `Guardian1Name`,
    CONCAT(`Guardian1`.`Address1`, ', ',
    IF(`Guardian1`.`Address2` = '', '', CONCAT(`Guardian1`.`Address2`, ', ')), `Guardian1`.`City`,
    ', ', `guardian1_state`.`StateAbbrev`,
    ' ', `Guardian1`.`ZipCode`)
    AS `Guardian1Address`,
    `Guardian1`.`HomePhone1` AS `Guardian1Phone`,
    CASE `Guardian1`.`NameFormat`
    WHEN 1 THEN
    `Guardian1`.`WorkPhone1`
    ELSE
    CONCAT(IF(`Guardian1`.`WorkPhone1` <> '', CONCAT(`Guardian1`.`FirstName1`, ': ', `Guardian1`.`WorkPhone1`), ''),
    IF(`Guar

    L M L 3 Replies Last reply
    0
    • P programmervb netc

      Ideas of how to improve this query

      SELECT `UniqueNumber-900` AS `ClientNumber`,
      (SELECT `AdmitDate`
      FROM `cl_clientsvcs_dayhab`
      WHERE `ClientCommonNumber` = `cl_client_common`.`UniqueNumber-900`
      ORDER BY `AdmitDate` ASC
      LIMIT 1)
      AS `InitialAdmitDate`,
      CONCAT(`cl_client_common`.`LastName`, ', ', `cl_client_common`.`FirstName`, ' ', `cl_client_common`.`MiddleName`)
      AS `ClientName`,
      `xx_component_master`.`ComponentCode`,
      `MedicaidNumber`,
      `MedicareNumber`,
      CONCAT(`cl_client_common`.`Address1`, IF(`cl_client_common`.`Address2` = '', '', CONCAT(', ', `cl_client_common`.`Address2`)))
      AS `Address1`,
      CONCAT(`cl_client_common`.`City`, ', ', `client_state`.`StateAbbrev`, ' ', `cl_client_common`.`ZipCode`)
      AS `CityStateZip`,
      CONCAT(`cl_client_common`.`PhoneNumber`, IF(`cl_client_common`.`PhoneNumber2` <> '', CONCAT('; ', `cl_client_common`.`PhoneNumber2`), ''))
      AS `ClientPhone`,
      `cl_client_common`.`PhoneNumber2` AS `ClientPhone2`,
      `cl_client_common`.`DateOfBirth`,
      `Gender`,
      `cl_client_common`.`SocialSecurityNumber`,
      `DDSNumber`,
      LEFT(`xx_marital_status`.`Description`, 1) AS `MaritalStatus`,
      LEFT(`xx_race_master`.`Description`, 1) AS `Race`,
      `xx_legal_status`.`Description` AS `LegalStatus`,
      CONCAT(`xx_sc_master`.`LastName`, ', ', `xx_sc_master`.`FirstName`) AS `ServiceCoordinator`,
      `cl_clientsvcs_dayhab`.`AdmitDate`,
      `cl_client_guardians`.`SelfGuardian`,
      `xx_county_master`.`CountyName`,
      CASE `Guardian1`.`NameFormat`
      WHEN 1 THEN
      CONCAT(`Guardian1`.`FirstName1`, ' ', `Guardian1`.`LastName1`)
      WHEN 2 THEN
      CONCAT(`Guardian1`.`FirstName1`, ' & ', `Guardian1`.`FirstName2`, ' ', `Guardian1`.`LastName1`)
      WHEN 3 THEN
      CONCAT(`Guardian1`.`FirstName1`, ' ', `Guardian1`.`LastName1`, ' & ', `Guardian1`.`FirstName2`,
      ' ', `Guardian1`.`LastName2`)
      END
      AS `Guardian1Name`,
      CONCAT(`Guardian1`.`Address1`, ', ',
      IF(`Guardian1`.`Address2` = '', '', CONCAT(`Guardian1`.`Address2`, ', ')), `Guardian1`.`City`,
      ', ', `guardian1_state`.`StateAbbrev`,
      ' ', `Guardian1`.`ZipCode`)
      AS `Guardian1Address`,
      `Guardian1`.`HomePhone1` AS `Guardian1Phone`,
      CASE `Guardian1`.`NameFormat`
      WHEN 1 THEN
      `Guardian1`.`WorkPhone1`
      ELSE
      CONCAT(IF(`Guardian1`.`WorkPhone1` <> '', CONCAT(`Guardian1`.`FirstName1`, ': ', `Guardian1`.`WorkPhone1`), ''),
      IF(`Guar

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Do you really expect anyone to read and study all that, while you are not even providing a decent description of what it does, what it is about, why you think it should run faster, and what you tried to get it any faster? :~

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

      P 1 Reply Last reply
      0
      • L Luc Pattyn

        Do you really expect anyone to read and study all that, while you are not even providing a decent description of what it does, what it is about, why you think it should run faster, and what you tried to get it any faster? :~

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

        P Offline
        P Offline
        programmervb netc
        wrote on last edited by
        #3

        Actually it runs fine. There was a post in the lounge regarding this particular statement and rather than post it in the lounge I posted it here and linked it. What is does is get information for a facesheet which is basically all information on a client. Like I said performance is not a problem there was just a conversation about the code being incorrect if it was so many lines of code. Humble Programmer

        1 Reply Last reply
        0
        • P programmervb netc

          Ideas of how to improve this query

          SELECT `UniqueNumber-900` AS `ClientNumber`,
          (SELECT `AdmitDate`
          FROM `cl_clientsvcs_dayhab`
          WHERE `ClientCommonNumber` = `cl_client_common`.`UniqueNumber-900`
          ORDER BY `AdmitDate` ASC
          LIMIT 1)
          AS `InitialAdmitDate`,
          CONCAT(`cl_client_common`.`LastName`, ', ', `cl_client_common`.`FirstName`, ' ', `cl_client_common`.`MiddleName`)
          AS `ClientName`,
          `xx_component_master`.`ComponentCode`,
          `MedicaidNumber`,
          `MedicareNumber`,
          CONCAT(`cl_client_common`.`Address1`, IF(`cl_client_common`.`Address2` = '', '', CONCAT(', ', `cl_client_common`.`Address2`)))
          AS `Address1`,
          CONCAT(`cl_client_common`.`City`, ', ', `client_state`.`StateAbbrev`, ' ', `cl_client_common`.`ZipCode`)
          AS `CityStateZip`,
          CONCAT(`cl_client_common`.`PhoneNumber`, IF(`cl_client_common`.`PhoneNumber2` <> '', CONCAT('; ', `cl_client_common`.`PhoneNumber2`), ''))
          AS `ClientPhone`,
          `cl_client_common`.`PhoneNumber2` AS `ClientPhone2`,
          `cl_client_common`.`DateOfBirth`,
          `Gender`,
          `cl_client_common`.`SocialSecurityNumber`,
          `DDSNumber`,
          LEFT(`xx_marital_status`.`Description`, 1) AS `MaritalStatus`,
          LEFT(`xx_race_master`.`Description`, 1) AS `Race`,
          `xx_legal_status`.`Description` AS `LegalStatus`,
          CONCAT(`xx_sc_master`.`LastName`, ', ', `xx_sc_master`.`FirstName`) AS `ServiceCoordinator`,
          `cl_clientsvcs_dayhab`.`AdmitDate`,
          `cl_client_guardians`.`SelfGuardian`,
          `xx_county_master`.`CountyName`,
          CASE `Guardian1`.`NameFormat`
          WHEN 1 THEN
          CONCAT(`Guardian1`.`FirstName1`, ' ', `Guardian1`.`LastName1`)
          WHEN 2 THEN
          CONCAT(`Guardian1`.`FirstName1`, ' & ', `Guardian1`.`FirstName2`, ' ', `Guardian1`.`LastName1`)
          WHEN 3 THEN
          CONCAT(`Guardian1`.`FirstName1`, ' ', `Guardian1`.`LastName1`, ' & ', `Guardian1`.`FirstName2`,
          ' ', `Guardian1`.`LastName2`)
          END
          AS `Guardian1Name`,
          CONCAT(`Guardian1`.`Address1`, ', ',
          IF(`Guardian1`.`Address2` = '', '', CONCAT(`Guardian1`.`Address2`, ', ')), `Guardian1`.`City`,
          ', ', `guardian1_state`.`StateAbbrev`,
          ' ', `Guardian1`.`ZipCode`)
          AS `Guardian1Address`,
          `Guardian1`.`HomePhone1` AS `Guardian1Phone`,
          CASE `Guardian1`.`NameFormat`
          WHEN 1 THEN
          `Guardian1`.`WorkPhone1`
          ELSE
          CONCAT(IF(`Guardian1`.`WorkPhone1` <> '', CONCAT(`Guardian1`.`FirstName1`, ': ', `Guardian1`.`WorkPhone1`), ''),
          IF(`Guar

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          Create a view so no other poor bugger has to look at this code again. I would probably break it into a number of different procs servicing different parts of the profile. Supporting this query would be nasty. What happens when the dietician want the name and diet details, does he get all of the face sheet.

          Never underestimate the power of human stupidity RAH

          P 2 Replies Last reply
          0
          • M Mycroft Holmes

            Create a view so no other poor bugger has to look at this code again. I would probably break it into a number of different procs servicing different parts of the profile. Supporting this query would be nasty. What happens when the dietician want the name and diet details, does he get all of the face sheet.

            Never underestimate the power of human stupidity RAH

            P Offline
            P Offline
            programmervb netc
            wrote on last edited by
            #5

            In the case of the dietician needs info we would generally do that on a different report with only the information needed by them. I do think that breaking the SQL out to bring in only parts of the profile is a good idea then we could more easily reuse them. However a concern of mine in doing that would be that if we have to tie all of these back together would that not be a pretty big performance hit? I would think you would have to do that with multiple statements which is in turn more network traffic. We try to do as much as we can with minimal hits to our DB because we have some users that are on very slow connections. Thanks for the ideas and look forward to more. Humble Programmer

            1 Reply Last reply
            0
            • P programmervb netc

              Ideas of how to improve this query

              SELECT `UniqueNumber-900` AS `ClientNumber`,
              (SELECT `AdmitDate`
              FROM `cl_clientsvcs_dayhab`
              WHERE `ClientCommonNumber` = `cl_client_common`.`UniqueNumber-900`
              ORDER BY `AdmitDate` ASC
              LIMIT 1)
              AS `InitialAdmitDate`,
              CONCAT(`cl_client_common`.`LastName`, ', ', `cl_client_common`.`FirstName`, ' ', `cl_client_common`.`MiddleName`)
              AS `ClientName`,
              `xx_component_master`.`ComponentCode`,
              `MedicaidNumber`,
              `MedicareNumber`,
              CONCAT(`cl_client_common`.`Address1`, IF(`cl_client_common`.`Address2` = '', '', CONCAT(', ', `cl_client_common`.`Address2`)))
              AS `Address1`,
              CONCAT(`cl_client_common`.`City`, ', ', `client_state`.`StateAbbrev`, ' ', `cl_client_common`.`ZipCode`)
              AS `CityStateZip`,
              CONCAT(`cl_client_common`.`PhoneNumber`, IF(`cl_client_common`.`PhoneNumber2` <> '', CONCAT('; ', `cl_client_common`.`PhoneNumber2`), ''))
              AS `ClientPhone`,
              `cl_client_common`.`PhoneNumber2` AS `ClientPhone2`,
              `cl_client_common`.`DateOfBirth`,
              `Gender`,
              `cl_client_common`.`SocialSecurityNumber`,
              `DDSNumber`,
              LEFT(`xx_marital_status`.`Description`, 1) AS `MaritalStatus`,
              LEFT(`xx_race_master`.`Description`, 1) AS `Race`,
              `xx_legal_status`.`Description` AS `LegalStatus`,
              CONCAT(`xx_sc_master`.`LastName`, ', ', `xx_sc_master`.`FirstName`) AS `ServiceCoordinator`,
              `cl_clientsvcs_dayhab`.`AdmitDate`,
              `cl_client_guardians`.`SelfGuardian`,
              `xx_county_master`.`CountyName`,
              CASE `Guardian1`.`NameFormat`
              WHEN 1 THEN
              CONCAT(`Guardian1`.`FirstName1`, ' ', `Guardian1`.`LastName1`)
              WHEN 2 THEN
              CONCAT(`Guardian1`.`FirstName1`, ' & ', `Guardian1`.`FirstName2`, ' ', `Guardian1`.`LastName1`)
              WHEN 3 THEN
              CONCAT(`Guardian1`.`FirstName1`, ' ', `Guardian1`.`LastName1`, ' & ', `Guardian1`.`FirstName2`,
              ' ', `Guardian1`.`LastName2`)
              END
              AS `Guardian1Name`,
              CONCAT(`Guardian1`.`Address1`, ', ',
              IF(`Guardian1`.`Address2` = '', '', CONCAT(`Guardian1`.`Address2`, ', ')), `Guardian1`.`City`,
              ', ', `guardian1_state`.`StateAbbrev`,
              ' ', `Guardian1`.`ZipCode`)
              AS `Guardian1Address`,
              `Guardian1`.`HomePhone1` AS `Guardian1Phone`,
              CASE `Guardian1`.`NameFormat`
              WHEN 1 THEN
              `Guardian1`.`WorkPhone1`
              ELSE
              CONCAT(IF(`Guardian1`.`WorkPhone1` <> '', CONCAT(`Guardian1`.`FirstName1`, ': ', `Guardian1`.`WorkPhone1`), ''),
              IF(`Guar

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              programmervb.netc++ wrote:

              Ideas of how to improve this query

              Shoot the person who wrote it!

              Just say 'NO' to evaluated arguments for diadic functions! Ash

              P 1 Reply Last reply
              0
              • L Lost User

                programmervb.netc++ wrote:

                Ideas of how to improve this query

                Shoot the person who wrote it!

                Just say 'NO' to evaluated arguments for diadic functions! Ash

                P Offline
                P Offline
                programmervb netc
                wrote on last edited by
                #7

                Very constructive thanks. Humble Programmer

                1 Reply Last reply
                0
                • M Mycroft Holmes

                  Create a view so no other poor bugger has to look at this code again. I would probably break it into a number of different procs servicing different parts of the profile. Supporting this query would be nasty. What happens when the dietician want the name and diet details, does he get all of the face sheet.

                  Never underestimate the power of human stupidity RAH

                  P Offline
                  P Offline
                  programmervb netc
                  wrote on last edited by
                  #8

                  I do have another question. As you can see we try to do as much logic as possible in the SQL so that we do not have do manipulate the dataset once it is returned.... Because from my understanding the server will be able to optimize much more efficiently than you will most likely be able to do in your program. For example rather than retrieve first and last name then concat as needed for the result just write it into the SQL so that your result set will already be correct. Humble Programmer

                  M 1 Reply Last reply
                  0
                  • P programmervb netc

                    I do have another question. As you can see we try to do as much logic as possible in the SQL so that we do not have do manipulate the dataset once it is returned.... Because from my understanding the server will be able to optimize much more efficiently than you will most likely be able to do in your program. For example rather than retrieve first and last name then concat as needed for the result just write it into the SQL so that your result set will already be correct. Humble Programmer

                    M Offline
                    M Offline
                    Mycroft Holmes
                    wrote on last edited by
                    #9

                    programmervb.netc++ wrote:

                    first and last name then concat as needed for the result just write it into the SQL

                    That is definitely reasonable, however you need to make a judgment call on how much formatting you do in the query. EG you may note the guy who wants to add CRLF to the data in the query, this is totally invalid whereas concat of the names is good. It also depends on the volume of data you are delivering, if you are doing this profile for 1 patient then this query is almost valid (I would still split it out for support reasons) doing this for 1000s is not valid.

                    Never underestimate the power of human stupidity RAH

                    P 1 Reply Last reply
                    0
                    • M Mycroft Holmes

                      programmervb.netc++ wrote:

                      first and last name then concat as needed for the result just write it into the SQL

                      That is definitely reasonable, however you need to make a judgment call on how much formatting you do in the query. EG you may note the guy who wants to add CRLF to the data in the query, this is totally invalid whereas concat of the names is good. It also depends on the volume of data you are delivering, if you are doing this profile for 1 patient then this query is almost valid (I would still split it out for support reasons) doing this for 1000s is not valid.

                      Never underestimate the power of human stupidity RAH

                      P Offline
                      P Offline
                      programmervb netc
                      wrote on last edited by
                      #10

                      Why would the number of clients really matter if your indexes are correct? I understand that the record set has an affect on total time but I don't see how that really relates to a CONCAT for example. If you are going to have to CONCAT last and first name on 1000 clients what benefit would you have from doing it locally instead of the server? Also in either case the concatenation I think the concatenation would be happening in memory so in theory the server should have more... and I would think that the CONCAT statement written in my case mySQL is probably more efficient than the equivalent code in .NET. Thanks for your thoughts I do appreciate your input just want to have a full understanding of your reasoning. Humble Programmer

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups