Thanks, I think there's something wrong with my code  :-D  (so it was slow).

I'm sorry I posted 2 same threads. I didn't know that my posts would be 
reviewed. And after I clicked Post there was no message saying that. I 
tried to delete one of the posts but to no successful. I'll edit that post 
to link the answer to this one.

On Wednesday, November 1, 2017 at 5:40:48 AM UTC+7, Jeremy Evans wrote:
>
> On Tuesday, October 31, 2017 at 7:16:44 PM UTC+1, Lincoln Hawk wrote:
>>
>> Thank you very much Jeremy.
>>
>> I changed to this:
>>
>> @db[:threads]
>>     .where(:user_id => 9)
>>     .exclude(:id => @db[:posts].where(:user_id => 9).select(:thread_id))
>>     .delete()
>>
>> And it runs as fast as raw SQL.
>>
>> Just a feedback, for my first version without distinct(), it was still 
>> very slow. From the principle of least astonishment 
>> <https://en.wikipedia.org/wiki/Principle_of_least_astonishment>, I think 
>> it should work like the raw SQL.
>>
>
> Your first version without distinct and this code result in exactly the 
> same SQL here modulo the order of the WHERE clause, so they should perform 
> the same:
>
> @db[:threads] .where(:user_id => 9) .exclude(:id => 
> @db[:posts].where(:user_id => 9).select(:thread_id)) .delete()
> # DELETE FROM threads WHERE ((user_id = 9) AND (id NOT IN (SELECT 
> thread_id FROM posts WHERE (user_id = 9))))
>
> @db[:threads] .where(:id => @db[:posts].where(:user_id => 
> 9).select(:thread_id)) .invert() .where(:user_id => 9) .delete()
> # DELETE FROM threads WHERE ((id NOT IN (SELECT thread_id FROM posts WHERE 
> (user_id = 9))) AND (user_id = 9))
>
> Thanks,
> Jeremy
>

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to