Re: Speed up ado updates
- From: Jano Svitok <jan.svitok@xxxxxxxxx>
- Date: Wed, 26 Dec 2007 19:00:07 -0500
On Dec 26, 2007 11:34 PM, lrlebron@xxxxxxxxx <lrlebron@xxxxxxxxx> wrote:
I have a script that uses ado to update a database. The script works
but it is very slow.
require 'win32ole'
data_source = "C:\path\F1Backup.sdf"
prefix = "E:\user\"
begin
db = WIN32OLE.new("ADODB.Connection")
db.open("Provider=Microsoft.SQLSERVER.MOBILE.OLEDB.3.0;Data
Source=#{data_source}")
rescue Exception => ex
puts ex.to_s
exit
end
sql_string=<<End_of_String
Select FilePath from catalog WHERE ParentDirectory Is Null
End_of_String
rs = db.execute(sql_string)
rs.MoveFirst
while !rs.eof
rs.fields.each do |field|
file_path = field.value.to_s.gsub("'", "''")
string = file_path.split('__--')[0].gsub(prefix,'')
parent_directory = string.slice(0,string.rindex("\
\")).insert(1,':')
sql_update = "Update catalog set
ParentDirectory='#{parent_directory}' WHERE FilePath='#{file_path}'"
puts sql_update
db.execute(sql_update)
end
rs.MoveNext
end
Any ideas on how to make it run faster?
thanks,
Luis
If you run the script under ruby-prof -p graph <your_script_name.rb>
you'll find out what parts
of the script take the most of the time. You'll find out whether you
should optimize the ruby part, or the query itself (ADO part).
Here you'll find explanation of the ruby-prof output:
http://on-ruby.blogspot.com/2006/08/ruby-prof-and-call-graphs.html
Notes:
1. I'd make data_source and prefix constants (capital letters, this
might actually hurt the performance -- it depends on whether const
lookup is faster than local var lookup or the other way round -- you
can find out yourself using Benchmark class)
2. You need to escape \ in string literals
DATA_SOURCE = "C:\\path\\F1Backup.sdf"
PREFIX = "E:\\user\\"
3. you might get some performance gains by making some of the
gsub/inserts in place ("!" versions -- gsub!, you'd need to change
your code to accomodate for the change)
4. something might be gained by replacing prefix/PREFIX with a Regex
and fixing its start (PREFIX = /^E:\\user\\/), when it indeed is a
prefix
(again, verify my guess with Benchmark)
.
- References:
- Speed up ado updates
- From: lrlebron@xxxxxxxxx
- Speed up ado updates
- Prev by Date: Re: RDocTask and SVN
- Next by Date: Re: Purpose of Ruby 1.9?
- Previous by thread: Speed up ado updates
- Next by thread: Re: Speed up ado updates
- Index(es):
Relevant Pages
|