mysql - How to run this complex query on millions of rows -


i have millions of rows of message.rb

i have migration i'm trying run:

add_index :messages, [:mm_id, :c_id, :s_id], unique: true, name: :mm_unique 

however migration failing because finding duplicate rows. don't care value of s_id (as long integer) , s_id , mm_id need unique in scope of c_id

so need find messages mm_id not null , mm_id, s_id, , c_id have duplicate rows.

then, how can write query fix quickly? i'd set random value on s_id long integer since dont care value is.

your query should find duplicate rows :

message.joins("inner join                 ( select mm_id, s_id, c_id, count(*) total_count                  messages group mm_id, s_id, , c_id                  having  count(*) >= 2                ) b on messages.mm_id = b.mm_id ,                  messages.s_id = b.s_id , messages.c_id = b.c_id"              )         .where("messages.mm_id not null")         .select("messages.*, b.total_count duplicate") 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -