Do not use on any
shared computer
August 29, 2008 07:08pm pdt
 
[x]
Attachment Details

how to improve this code or reason for getting answers from queries slow??

Tags: foxpro
DEFINE CLASS sqlvsvfp as Session OLEPUBLIC
     PROCEDURE Init()
          LOCAL nCtr as Integer
          PUBLIC sqlConnStr
          sqlConnStr      = SQLStringConnect("Driver=SQL Server;Server=;"+ ;
                "DSN=TNS_SL;UID=;PWD=;Database=;Trusted_Connection=Yes")
     
          =SYS(2339,0)

             = SYS(2023)

          SET EXCLUSIVE OFF
          SET DELETED ON
          SET PROCEDURE TO d:\progs\library.prg ADDITIVE
         
          * Open the log table. This is a shortened version of the UseTable() program in d:\progs\library
          nCtr = 1
          DO WHILE .T.
               IF nCtr > 10 OR sqlConnStr > 0
                    EXIT
               ENDIF
               TRY
                    * If this failed 10 times then exit. No use trying over and over again.
                    * This is a implicit ELSE for the statement above.
                    sqlConnStr      = SQLStringConnect("Driver=SQL Server;Server=;"+ ;
                     "DSN=TNS_SL;UID=;PWD=;Database=;Trusted_Connection=Yes")                    
               CATCH TO oException
                    nCtr = nCtr + 1
               ENDTRY
          ENDDO
         
          * If the maximum # of attempts was reached then exit now.
          IF nCtr > 10 OR sqlConnStr < 0
               DO email_error WITH "Service Log Error",LINENO( ),"Could not open servicelog table "+this.cLogFile,MESSAGE(1),.F.
               lIsReady = .F.
          ELSE
               lIsReady = .T.
               RETURN .T.
          ENDIF
     ENDPROC

     PROCEDURE insertDataSQL(sName as String,sAge as String)
          *SQLEXEC(sqlConnStr,"INSERT INTO test(name,age) values (?sName,?sAge)")
          SQLEXEC(sqlConnStr,"EXEC insertdata ?sName,?sAge")
     ENDproc
     
     PROCEDURE selectDataSQL()
          SQLEXEC(sqlConnStr,"select COUNT(*) from test")
     ENDPROC
     
     PROCEDURE Destroy()
          LOCAL sqlDisChkStr
          sqlDisChkStr =     SQLDISCONNECT(sqlConnStr)
          IF sqlDisChkStr = 1
               MESSAGEBOX("Disconnected from testservicelog")
          ELSE
               MESSAGEBOX(" Still connected")
               sqlDisChkStr = SQLDISCONNECT(0)
               IF !(sqlDisChkStr > 0)
                    MESSAGEBOX("DON KNOW HOW TO DISCONNECT THIS IS BAD")
               ENDIF
          ENDIF
     ENDPROC
         
     PROCEDURE Release()
          ON ERROR
          this.Destroy()
          RELEASE this
     ENDPROC      

ENDDEFINE

This is the sql code that i am testing.  The sqlConnStr connects to MS SQL server.  The time for inserting 100 data
using
FOR n = 1 to 100
       o.insertDataSql(name,age)
ENDFOR
was around 30.005

WHEREAS


o = CREATEOBJECT('sqlvsvfp')
testTime = SECONDS()
*     FOR age = 1 TO 100
*          o.insertDataSql('vader','1')
*     ENDFOR
          o.selectDataSQL()
?SECONDS()-testTime

DEFINE CLASS sqlvsvfp as Session OLEPUBLIC
     cLogFile = "d:\test.dbf"
     cTempPath = ""
     lIsReady = .T.          && Used to specify init failure to caller
     cError = ""
         
        DataSession = 2
     

     *ascmnt default constructor for ServiceLog
     PROCEDURE Init()
          LOCAL llerr,nCtr as Integer
          * llerr: For trapping errors
          * nCtr: A counter
         
          * Enable DLL cleanup
          =SYS(2339,0)

          * Set the temporary path
          *ascmnt Returns the path in which Visual FoxPro stores its temporary files.
          *ascmnt Character.SYS(2023) returns a path name
          this.cTempPath = SYS(2023)

          SET EXCLUSIVE OFF
          SET DELETED ON
          SET PROCEDURE TO d:\progs\library.prg ADDITIVE
          *ascmnt
          *ADDITIVE  makes it possible to update and add object properties.
          *To instantiate an OLEPUBLIC class, Visual FoxPro must be able to find all the code for the class. There is an internal
          *SET PROCEDURE/SET CLASSLIB to detect all the related code;
          *if you try to change this setting, an error occurs. To prevent this, use SET PROCEDURE TO with the ADDITIVE keyword.


          * Open the log table. This is a shortened version of the UseTable() program in d:\progs\library
          nCtr = 1
          DO WHILE .T.
               IF nCtr > 10 OR USED("servicelog")
                    EXIT
               ENDIF
               TRY
                    * If this failed 10 times then exit. No use trying over and over again.
                    * This is a implicit ELSE for the statement above.
                    USE (this.cLogFile) ALIAS servicelog SHARED
               CATCH TO oException
                    nCtr = nCtr + 1
               ENDTRY
          ENDDO
         
          * If the maximum # of attempts was reached then exit now.
          IF nCtr > 10
               DO email_error WITH "Service Log Error",LINENO( ),"Could not open servicelog table "+this.cLogFile,MESSAGE(1),.F.
               this.lIsReady = .F.
          ELSE
               this.lIsReady = .T.
               RETURN .T.
          ENDIF

     ENDPROC
     
     PROCEDURE insertDataSQL(sName as String,sAge as String)
          IF TYPE('sName') == "C" OR TYPE('sAge') == "C"
               INSERT INTO test(pname,ppAge) VALUES (sName,sAge)
          ELSE
               ?"this suck"
          ENDIF
     ENDproc
     
     PROCEDURE selectDataSQL()
          select COUNT(*) from test;
          into cursor xop          
     ENDPROC
     
     PROCEDURE Destroy()
          CLOSE DATABASES ALL
     ENDPROC
     
     
     PROCEDURE Release()
          ON ERROR
          this.Destroy()
          RELEASE this
     ENDPROC      

ENDDEFINE
It gives .201 using the same test for inserting data into the table.

For  selectDataSQL() for the sql one it took .428 and with the foxpro it took .011.

Can anyone give me advise? or tell me why there is so much time difference
Start your free trial to view this solution
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Question Stats
Zone: Microsoft
Question Asked By: aashishshrestha
Solution Provided By: tmedley
Participating Experts: 2
Solution Grade: A
Views: 0
Translate:
Loading Advertisement...
 
[+][-]Expert Comment by suhashegde

Rank: Master

Expert Comment by suhashegde:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Accepted Solution by tmedley

Rank: Master

Accepted Solution by tmedley:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
20080723-EE-VQP-34