B
B
bminaiev2011-07-28 15:41:48
PHP
bminaiev, 2011-07-28 15:41:48

Need advice on MySQL and Php (how to write a query correctly)?

There is a DB approximately on one million records at which 4 fields: name, summ, original, original_already. Initially, the first three fields are filled, you need to write the most time / memory-optimal query that will update the fourth field. For each record, original_already must equal 1 if at least one DB record that has the same name and summ values ​​as the current one has the value original=1. Otherwise, 0 should be written in the field.
The “head-on” solution (two nested loops) will not pass in time. I want the request/script to work for no more than an hour and download no more than 512M.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Ano, 2011-07-28
@Ano

i would try

BEGIN;
create temporary table TMP
  as select name, summ
    from THETABLE
    where original = 1
    group by name, summ;
update THETABLE set original_already = 0;
update THETABLE inner join TMP on TMP.name = THETABLE.name and TMP.summ = THETABLE.summ
  set THETABLE.original_already = 1
drop table TMP;
COMMIT;

A
Alexander Rudevich, 2011-07-28
@rudevich

"For each record, original_already must equal 1 if at least one database record that has the same name and summ values ​​as the current one has original=1." Russian is ok? ))

A
Anatoly, 2011-07-28
@taliban

If resources are critical = crowns + sampling 1000 rows at a time. Something like this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question