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. General Programming
  3. C / C++ / MFC
  4. problem with Open of a query to a recordset

problem with Open of a query to a recordset

Scheduled Pinned Locked Moved C / C++ / MFC
databasehelpcssperformancequestion
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.
  • S Offline
    S Offline
    SWDevil
    wrote on last edited by
    #1

    Hi, I have an application in which I am loading data from a database (in this specific case a large database) into a grid in my GUI. So, I open the query into a recordset using the Open method as follows: sQuery.Format(_T("SELECT DISTINCT D.fieldA, DC.fieldB from TableDC DC, TableD D WHERE D.fieldA = DC.fieldA AND D.No = '3’ ORDER BY DC.fieldB")); if(!Open(AFX_DB_USE_DEFAULT_TYPE, sQuery, CRecordset::readOnly)) { ... } The problem is that the statement that contains the call to the Open method sometimes takes about 2 minutes to execute (and sometimes works ok). Anyone have any idea of why this happens? And maybe an idea of how I can fix it (speed it up)?

    D V 2 Replies Last reply
    0
    • S SWDevil

      Hi, I have an application in which I am loading data from a database (in this specific case a large database) into a grid in my GUI. So, I open the query into a recordset using the Open method as follows: sQuery.Format(_T("SELECT DISTINCT D.fieldA, DC.fieldB from TableDC DC, TableD D WHERE D.fieldA = DC.fieldA AND D.No = '3’ ORDER BY DC.fieldB")); if(!Open(AFX_DB_USE_DEFAULT_TYPE, sQuery, CRecordset::readOnly)) { ... } The problem is that the statement that contains the call to the Open method sometimes takes about 2 minutes to execute (and sometimes works ok). Anyone have any idea of why this happens? And maybe an idea of how I can fix it (speed it up)?

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Are the TableD.fieldA and TableD.No fields indexed?


      "The largest fire starts but with the smallest spark." - David Crow

      "Judge not by the eye but by the heart." - Native American Proverb

      S 1 Reply Last reply
      0
      • S SWDevil

        Hi, I have an application in which I am loading data from a database (in this specific case a large database) into a grid in my GUI. So, I open the query into a recordset using the Open method as follows: sQuery.Format(_T("SELECT DISTINCT D.fieldA, DC.fieldB from TableDC DC, TableD D WHERE D.fieldA = DC.fieldA AND D.No = '3’ ORDER BY DC.fieldB")); if(!Open(AFX_DB_USE_DEFAULT_TYPE, sQuery, CRecordset::readOnly)) { ... } The problem is that the statement that contains the call to the Open method sometimes takes about 2 minutes to execute (and sometimes works ok). Anyone have any idea of why this happens? And maybe an idea of how I can fix it (speed it up)?

        V Offline
        V Offline
        Viorel
        wrote on last edited by
        #3

        As I now, execution of SQL queries can be speeded up by defining appropriate indexes in the database definition. I think you have to add indexes for TableD.fieldA, TableDC.fieldA, TableD.No and TableDC.fieldB. This can be done with the application you are using for database management. In additions, use the DISTINCT statement only if is realy required.

        S 1 Reply Last reply
        0
        • D David Crow

          Are the TableD.fieldA and TableD.No fields indexed?


          "The largest fire starts but with the smallest spark." - David Crow

          "Judge not by the eye but by the heart." - Native American Proverb

          S Offline
          S Offline
          SWDevil
          wrote on last edited by
          #4

          I don't think they are. Not in table D at least. What exactly do you mean by indexed?

          D 1 Reply Last reply
          0
          • S SWDevil

            I don't think they are. Not in table D at least. What exactly do you mean by indexed?

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            SWDevil wrote:

            What exactly do you mean by indexed?

            See here.


            "The largest fire starts but with the smallest spark." - David Crow

            "Judge not by the eye but by the heart." - Native American Proverb

            S 1 Reply Last reply
            0
            • D David Crow

              SWDevil wrote:

              What exactly do you mean by indexed?

              See here.


              "The largest fire starts but with the smallest spark." - David Crow

              "Judge not by the eye but by the heart." - Native American Proverb

              S Offline
              S Offline
              SWDevil
              wrote on last edited by
              #6

              ok - I think I confused indexes and primary keys :) do you know how I can see in SQL server if the fields are indexed or not? And how can I use indexes on these fields to speed the execution of the open method?

              D 1 Reply Last reply
              0
              • V Viorel

                As I now, execution of SQL queries can be speeded up by defining appropriate indexes in the database definition. I think you have to add indexes for TableD.fieldA, TableDC.fieldA, TableD.No and TableDC.fieldB. This can be done with the application you are using for database management. In additions, use the DISTINCT statement only if is realy required.

                S Offline
                S Offline
                SWDevil
                wrote on last edited by
                #7

                Do you know how to add indexes to fields in SQL Server?

                D M 2 Replies Last reply
                0
                • S SWDevil

                  Do you know how to add indexes to fields in SQL Server?

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  SWDevil wrote:

                  Do you know how to add indexes to fields in SQL Server?

                  Use the CREATE INDEX command.


                  "The largest fire starts but with the smallest spark." - David Crow

                  "Judge not by the eye but by the heart." - Native American Proverb

                  1 Reply Last reply
                  0
                  • S SWDevil

                    ok - I think I confused indexes and primary keys :) do you know how I can see in SQL server if the fields are indexed or not? And how can I use indexes on these fields to speed the execution of the open method?

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    SWDevil wrote:

                    do you know how I can see in SQL server if the fields are indexed or not?

                    Have you considered the Index Tuning Wizard? There's also SHOWPLAN_TEXT and SHOWPLAN_ALL opptions.


                    "The largest fire starts but with the smallest spark." - David Crow

                    "Judge not by the eye but by the heart." - Native American Proverb

                    1 Reply Last reply
                    0
                    • S SWDevil

                      Do you know how to add indexes to fields in SQL Server?

                      M Offline
                      M Offline
                      Milton Karimbekallil
                      wrote on last edited by
                      #10

                      Use the index tuning wizard in the Query Analyzer. This will give you the right index creation scripts optimized for your select statement. cheers...milton kb.

                      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