Jump to content

Efficiency of code


ajit
 Share

Go to solution Solved by jchd,

Recommended Posts

Local $query = "(Select COUNT (*) From Files)"
$adoRs.Open($query, $adoCon)
$Parray = $adoRs.GetRows()
$adoRs.Close
Local $iRows_id = $Parray[0][0]

_GUICtrlListView_DeleteAllItems($listview_Files)

If $iRows_id <> 0 Then
    For $i = 1 To $iRows_id
        Local $query = "(Select * From Files)"
        $adoRs.Open($query, $adoCon)
        $Parray = $adoRs.GetRows()
        GUICtrlCreateListViewItem($Parray[$i - 1][1] & "|" & $Parray[$i - 1][2] & "|" & $Parray[$i - 1][3] & "|" & $Parray[$i - 1][4] & "|" & $Parray[$i - 1][5] & "|" & $Parray[$i - 1][6] & "|" & $Parray[$i - 1][7], $listview_Files)
        $adoRs.Close
    Next
EndIf

Hi,

Please can someone have a look at my code to see if is efficient to work with large number of records, either at the database query part or GUICtrlCreateListViewItem part.

I have uploaded part of the code which I think may be inefficient.

Would be grateful for any suggestions.

Regards,

Ajit

Link to comment
Share on other sites

  • Solution

You're issuing the bulk query in the loop, which is uncalled for: run it once, then process the array. Also querying the count beforehand is also pointless. So this should work:

Local $query = "(Select * From Files)"
$adoRs.Open($query, $adoCon)
$Parray = $adoRs.GetRows()
$adoRs.Close

_GUICtrlListView_DeleteAllItems($listview_Files)

If UBound($Parray) <> 0 Then
    For $i = 0 To UBound($Parray) - 1
        GUICtrlCreateListViewItem($Parray[$i][1] & "|" & $Parray[$i][2] & "|" & $Parray[$i][3] & "|" & $Parray[$i][4] & "|" & $Parray[$i][5] & "|" & $Parray[$i][6] & "|" & $Parray[$i][7], $listview_Files)
    Next
EndIf

But if you think twice, you're concatenating the fields thru AutoIt code yet I'm ready to bet that the DB engine can directly provide the wanted output faster:

Local $query = "(Select col1 || '|' || col2 || '|' || col3 || '|' || col4 || '|' || col5 || '|' || col6 || '|' || col7 From Files)"
$adoRs.Open($query, $adoCon)
$Parray = $adoRs.GetRows()
$adoRs.Close

_GUICtrlListView_DeleteAllItems($listview_Files)

If UBound($Parray) <> 0 Then
    For $i = 0 To UBound($Parray) - 1
        GUICtrlCreateListViewItem($Parray[$i][0], $listview_Files)
    Next
EndIf

This way allows you to retrieve exactly the columns you want in the order of your choice with the proper separator ready to push into the new listview item.

Never hesitate to use SQL processing power within the query to obtain the simplest and most compact output, thus requiring less (slower) AutoIt code.

EDIT: you may have to adjust the test for an empty array, depending on how the ADO layer handles an empty resultset (empty array or empty string or error or something else).

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

@JCHD,

Thanks for the help, that speeds up the process.

Empty array was a problem so needed to query the count beforehand (application crashed if query returned an empty array).  I am using Access database if that matters.

Thanks again for the help.

Regards,

Ajit

Link to comment
Share on other sites

 

application crashed if query returned an empty array ...  I am using Access

Do you mean .GetRows() crashes or another error? An empty resultset shouldn't cause any problem with:

If UBound($Parray) Then

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

$dbname = "C:\Documents and Settings\Admin\Desktop\MSDataBase.mdb"
$adoCon = ObjCreate("ADODB.Connection")
$adoCon.Open("Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & $dbname) ;Use this line if using MS Access 2003 and lower
;~ $adoCon.Open ("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $dbname) ;Use this line if using MS Access 2007 and using the .accdb file extension
$adoRs = ObjCreate("ADODB.Recordset")
$adoRs.CursorType = 1
$adoRs.LockType = 3 

@JCHD,

$Parray = $adoRs.GetRows()
$Parray = $adoRs.GetRows()^ ERROR
 
GetRows() gives an error if I delete all records (empty array).
 
I have uploaded ADO part just in case.
 
Regards,
 
Ajit
Edited by ajit
Link to comment
Share on other sites

So there is no more surprise in 2015 than in 2014: Access is broken beyond repair.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...