mysql - Comparison Query For Last 1000 Records -
mysql - Comparison Query For Last 1000 Records -
i'm looking compare users in database based on lastly 1000 actions. basic construction of query follows:
select p.id, p2.id, round(avg(1000 + abs(p2.ss - p.ss)/6.1 * -50)) sim_score (select * p id = 519000 order p_date desc limit 0,1000) p, (select * p id = 279000 order p_date desc limit 0,1000) p2
in case specify both users (519000 , 279000). homecoming 1 record each id , similarity score. options returning 1 record every user comparing (u^2-u) in database, goal of dumping result in table? can see using nested cursors in nightly batch job, i'm hoping there more efficient.
you can create tables of lastly 1,000 actions each user using rank variable , grouping compare each user. notice status @ bottom create sure don't duplicate comparisons.
a note on efficiency: query in original question takes 1 sec execute. when adding 1 of tables below, execution time takes 30 minutes. sec table balloons query 450 minutes (i haven't confirmed). so, while reply works, i'm still looking works quicker.
select p.id, p2.id, round(avg(1000 + abs(p2.ss - p.ss)/6.1 * -50)) sim_score (select * ( select p.id, (case p.id when @curpit @currow := @currow + 1 else @currow := 1 , @curpit := p.id end) rank pitches p, (select @currow := 0, @curpit := '') r order p.id, p.p_date desc) n rank <= 1000) p, (select * ( select p.id, (case p.id when @curpit @currow := @currow + 1 else @currow := 1 , @curpit := p.id end) rank pitches p, (select @currow := 0, @curpit := '') r order p.id, p.p_date desc) n rank <= 1000) p2 p.id < p2.id grouping p.id, p2.id
mysql sql cursor query-optimization
Comments
Post a Comment