add in a test showing how to use async with mysql.use_result
This commit is contained in:
parent
e1556447a5
commit
7a0ecc3497
46
test/test_parsing_while_response_is_being_read.rb
Normal file
46
test/test_parsing_while_response_is_being_read.rb
Normal file
@ -0,0 +1,46 @@
|
||||
# This is an example of using Mysql::ResultSet#use_result [see docs for what that does]
|
||||
# this function is useful for those who have large query results and want to be able to parse them
|
||||
# as they come in, instead of having to wait for the query to finish before doing parsing
|
||||
# for me, running this on a query with 200_000 lines decreases total time to create an array of results
|
||||
# from .82s to .62s
|
||||
# you can experiment with it by changing the query here to be a long one, and toggling the do_the_use_query_optimization variable
|
||||
# this also has the interesting property of 'freeing' Ruby to do thread changes mid-query.
|
||||
|
||||
require 'mysqlplus'
|
||||
|
||||
do_the_use_query_optimization = true
|
||||
|
||||
$count = 5
|
||||
|
||||
$start = Time.now
|
||||
|
||||
$connections = []
|
||||
$count.times do
|
||||
$connections << Mysql.real_connect('localhost','root', '', 'local_leadgen_dev')
|
||||
end
|
||||
|
||||
puts 'connection pool ready'
|
||||
|
||||
$threads = []
|
||||
$count.times do |i|
|
||||
$threads << Thread.new do
|
||||
|
||||
puts "sending query on connection #{i}"
|
||||
conn = $connections[i]
|
||||
saved = []
|
||||
query = "select * from campus_zips"
|
||||
if do_the_use_query_optimization
|
||||
conn.query_with_result=false
|
||||
result = conn.async_query(query)
|
||||
result.use_result.each_hash { |h| saved << h }
|
||||
else
|
||||
conn.async_query(query).each_hash {|h| saved << h }
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
puts 'waiting on threads'
|
||||
$threads.each{|t| t.join }
|
||||
|
||||
puts Time.now - $start
|
Loading…
Reference in New Issue
Block a user