Carlos Sánchez Pérez
Currently working at @navandu_ COO, Co-founder at @leemurapp & @wearenominis exCTO @beruby_es. ExDev at @The_Cocktail & @aspgems
Codetails The Cocktail @carlossanchezp
Si ocurre esto es muy extraño, no es normal
Un full scan no es un problema, es un síntoma de que necesitas índices
FILESORT: WHERE ID = 123 ORDER BY NAME
Using INDEX: COUNT SUM AVG
mysql> explain extended select * from users order by idusers desc limit 20 ;
+----+-------------+-------+-------+---------------+---------+---------+------+------+-----------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-----------+-------+
| 1 | SIMPLE | users | index | NULL | PRIMARY | 4 | NULL | 20 | 396545.00 | |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-----------+-------+
Filtered = 396.545 and total rows = 79.309
The explain shows rows = 20
Filtered calculate = (Total Rows Table / 20)*100 = 396.545
Ya tenemos la solución....... ¿de verdad lo crees así?
Todo lo probamos y medimos en local, después en muchos casos en Staging también
# SOLICIÓN 1
con_sql.execute "START TRANSACTION" if cont == 0
sql = "select order_id from order_events_new_origin where order_id = #{oe.order_id}"
result = con_sql.execute sql
if !inserts.include? oe.order_id
puts "CREAMOS/MIGRAMOS en NUEVA TABLA Order ID #{oe.order_id} y el CONT #{cont}"
sql = "INSERT INTO order_events_new_origin
(order_id,origin_type,origin_id,kind,data,created_at,updated_at)
SELECT order_id,origin_type,origin_id,kind,data,created_at,updated_at
FROM order_events where order_id = '#{oe.order_id}'"
con_sql.execute sql
cont = cont + 1
inserts.push oe.order_id
end
# SOLUCIÓN 2
# Vamos a sanitizar los campos antes de llevarlos al insert
kind = oe.kind.nil? ? '' : oe.kind
s = oe.data.nil? ? ''.inspect : oe.data.inspect
# almacen de los values con sus datos ya sanitizados
if oe.origin_id.nil?
inserts_values.push "(#{oe.order_id.to_i},
'#{oe.origin_type}', null,
'#{kind}', #{s}, '#{oe.created_at.to_s(:db)}',
'#{oe.updated_at.to_s(:db)}')"
else
inserts_values.push "(#{oe.order_id.to_i},
'#{oe.origin_type}', #{oe.origin_id.to_i},
'#{kind}', #{s}, '#{oe.created_at.to_s(:db)}',
'#{oe.updated_at.to_s(:db)}')"
end
if inserts_values.size >= size_values
sql_insert = "INSERT INTO #{table_destination} #{columns_origin}
VALUES #{inserts_values.join(", ")}"
begin
con_sql.execute sql_insert
rescue Exception => e
puts "No lo guardamos ERROR #{e}"
end
inserts_values = []
end
¿Preguntas?
By Carlos Sánchez Pérez
@carlossanchezp
Currently working at @navandu_ COO, Co-founder at @leemurapp & @wearenominis exCTO @beruby_es. ExDev at @The_Cocktail & @aspgems