SQL Queries

--0016368e2a67324d23047b2fb0cd
Content-Type: text/plain; charset=UTF-8

Hey, Lets assume I got a table named "users".
It contains id & name.

I have another table called "notes" - which contains id, user_id, contents


I want to delete all users from table "users" that don't have notes (SELECT
.... FROM notes WHERE user_id=ID) returns empty result.


What is the fastest way to do it?

--
Use ROT26 for best security

--0016368e2a67324d23047b2fb0cd--
daniel danon [ So, 20 Dezember 2009 22:30 ] [ ID #2026615 ]

Re: SQL Queries

--00163691fbedd9132c047b2fd6bf
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Sorry for the double-post, forgot to add up the way I thought about using:

Simple sql query:

SELECT * FROM `users` as u WHERE (SELECT COUNT(id) FROM notes WHERE user_i=
d
=3D u.id LIMIT 0,1) =3D 0

Problem is I have about 450,000 "users" and about 90% don't have "notes",
and it takes LOADS of times even with I limit it:

SELECT * FROM `users` as u WHERE (SELECT COUNT(id) FROM notes WHERE user_i=
d
=3D u.id LIMIT 0,1) =3D 0 LIMIT 0,10

Takes about 10 seconds which is too much time... Any way to optimize it?

On Sun, Dec 20, 2009 at 11:30 PM, =D7=93=D7=A0=D7=99=D7=90=D7=9C =D7=93=D7=
=A0=D7=95=D7=9F <danondaniel [at] gmail.com> wrote:

> Hey, Lets assume I got a table named "users".
> It contains id & name.
>
> I have another table called "notes" - which contains id, user_id, content=
s
>
>
> I want to delete all users from table "users" that don't have notes (SELE=
CT
> ... FROM notes WHERE user_id=3DID) returns empty result.
>
>
> What is the fastest way to do it?
>
> --
> Use ROT26 for best security
>



--
Use ROT26 for best security

--00163691fbedd9132c047b2fd6bf--
daniel danon [ So, 20 Dezember 2009 22:41 ] [ ID #2026616 ]

Re: Re: SQL Queries

Hello,

That kind of queries usually run faster using a LEFT JOIN, like this:

select u.id
from users u
left join notes n on u.id =3D n.user_id
where n.id is null;

That query will give you the ids of the users without notes. Make sure
to have an index on notes.user_id to let the LEFT JOIN use it and run
faster.

Hope that helps, regards,

Jonathan

On Sun, Dec 20, 2009 at 6:41 PM, =D7=93=D7=A0=D7=99=D7=90=D7=9C =D7=93=D7=
=A0=D7=95=D7=9F <danondaniel [at] gmail.com> wrote:
> Sorry for the double-post, forgot to add up the way I thought about using=
:
>
> Simple sql query:
>
> SELECT * FROM `users` as u =C2=A0WHERE (SELECT COUNT(id) FROM notes WHERE=
user_id
> =3D u.id LIMIT 0,1) =3D 0
>
> Problem is I have about 450,000 "users" and about 90% don't have "notes",
> and it takes LOADS of times even with I limit it:
>
> SELECT * FROM `users` as u =C2=A0WHERE (SELECT COUNT(id) FROM notes WHERE=
user_id
> =3D u.id LIMIT 0,1) =3D 0 LIMIT 0,10
>
> Takes about 10 seconds which is too much time... Any way to optimize it?
>
> On Sun, Dec 20, 2009 at 11:30 PM, =D7=93=D7=A0=D7=99=D7=90=D7=9C =D7=93=
=D7=A0=D7=95=D7=9F <danondaniel [at] gmail.com> wrote:
>
>> Hey, Lets assume I got a table named "users".
>> It contains id & name.
>>
>> I have another table called "notes" - which contains id, user_id, conten=
ts
>>
>>
>> I want to delete all users from table "users" that don't have notes (SEL=
ECT
>> ... FROM notes WHERE user_id=3DID) returns empty result.
>>
>>
>> What is the fastest way to do it?
>>
>> --
>> Use ROT26 for best security
>>
>
>
>
> --
> Use ROT26 for best security
>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jonathan Tapicer [ So, 20 Dezember 2009 23:07 ] [ ID #2026617 ]

Re: SQL Queries

You should be able to do this from within the query. Try the following =
query:

DELETE users.*
FROM users
LEFT JOIN notes
USING(user_id)
WHERE notes.note_id IS NULL

Take care,
Floyd

On Dec 20, 2009, at 4:30 PM, =D7=93=D7=A0=D7=99=D7=90=D7=9C =D7=93=D7=A0=D7=
=95=D7=9F wrote:

> Hey, Lets assume I got a table named "users".
> It contains id & name.
>
> I have another table called "notes" - which contains id, user_id, =
contents
>
>
> I want to delete all users from table "users" that don't have notes =
(SELECT
> ... FROM notes WHERE user_id=3DID) returns empty result.
>
>
> What is the fastest way to do it?
>
> --
> Use ROT26 for best security


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Floyd Resler [ Mo, 21 Dezember 2009 14:46 ] [ ID #2026705 ]
PHP » gmane.comp.php.general » SQL Queries

Vorheriges Thema: TCPDF line breaks
Nächstes Thema: PHP and SEO Workarounds