» »

MySql

MySql

BRBR ::


DELETE FROM  detail WHERE id IN
(
SELECT MAX(id) AS id
       FROM master
       GROUP BY term
       HAVING  COUNT(term) > 1
)			  


Zakaj tale melje 100 let, čakam 20 min pa ni rezultata. Inner da rezultat v 6 sec, +/-20 recordov
outer ima +/- 300.000 recordov
  • spremenil: BRBR ()

kuall ::

The EXISTS clause is faster than IN when the subquery results are very large. The IN clause is faster than EXISTS when the subquery results are very small.

jaz nikoli ne uporabljam in, vedno exists, pa programiram v sql vsak dan. in je neuporaben, exists je hitrejša alternativa. to vse od takrat, ko sem prebral, da je exists hitrejši in tudi intuicija ti lahko govori, da je exists hitrejši. in vsakič zbere vse rezultate, ko izbriše eno vrstico. exists pa neha takoj, ko najde prvo.

BRBR ::

OK, dela bliskovito, ....

In kako to deluje z delete, će v delete part ni kolumna, ki je v inner part

DELETE FROM  detail WHERE EXISTS 
(
SELECT MAX(id) AS id
       FROM master
       GROUP BY term
       HAVING  COUNT(term) > 1
)			


Tale zbriše kar komplet vse v tabeli. V čem je fora ?

Inner part da ven 20 id-jev.

Zgodovina sprememb…

  • spremenil: BRBR ()

OracleDev ::

Pomoje samo uporab aliase za tabele in stolpce v prvem sqlu in bo delal.

kuall ::

Nekam v inner sql moraš vštulit where detail.id je master.id. čeprav bi jaz raje naredu cte iz inner sql in join nanj.nisem zihr ali je exists tu ok rešitev.

klemenSLO ::

KAJ TAKEGA?

DELETE FROM detail D
JOIN
--WHERE EXISTS
(
SELECT MAX(id) AS id
FROM master
GROUP BY term
HAVING COUNT(term) > 1
) X ON X.ID =D.ID
Life is not measured in minutes, but in MOMENTS...

OracleDev ::

Ne, tole ne sme it v nobeno kodo

kuall ::

težko kodirat brez tabel ampak kaj takega bo najbrž potrebno:
;WITH CTE AS
(
	SELECT MAX(id) AS id
	FROM master m
	GROUP BY m.term
	HAVING  COUNT(term) > 1
)
SELECT * 
-- DELETE d
FROM detail d
WHERE EXISTS (SELECT 1 FROM CTE c WHERE d.id = c.id)

Zgodovina sprememb…

  • spremenilo: kuall ()

DamijanD ::

Ne vem, če mysql podpira to notacijo ampak mogoče bi lahko tako poskusil:

with cte as (SELECT MAX(id) AS id
FROM master m
GROUP BY m.term
HAVING COUNT(term) > 1
)
delete e
from master e
inner join cte on cte.id = e.id

na ms sql to deluje

exists tukaj ni ok pristop imho, ker ti hočeš izbrisati največji ID od "duplikatov"

kuall ::

Kje imaš pa detail tablo?

DamijanD ::

my bad:
with cte as (SELECT MAX(id) AS id
FROM master m
GROUP BY m.term
HAVING COUNT(term) > 1
)
delete e
from detail e
inner join cte on cte.id = e.id

kuall ::

jp join bo kr pravi kle, exists/in ni potreben tu.
sem malo pobrkljal oba možna načina:

NAČIN 1:
;with cte as
(
   select key 
   from detail oi 
   where oi.product like '%z%' 
   group by key 
   having COUNT (oi.product) > 1
)
select * 
from master o 
join cte c on c.key = o.key


NAČIN 2:
select * 
from master o 
where (select COUNT (1) from detail oi2 where oi2.key = o.key and oi2.product like '%z%') > 1



Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

SQL

Oddelek: Programiranje
81097 (750) no comment
»

Učenje programiranja PHP

Oddelek: Programiranje
91483 (1024) Spura
»

sql, negacija ..

Oddelek: Programiranje
6717 (612) BRBR
»

Normalizirana struktura - query

Oddelek: Programiranje
191721 (1341) frudi
»

MYSQL vprašanje

Oddelek: Programiranje
131790 (1405) MrBrdo

Več podobnih tem