<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: gyaaniguy</title>
    <description>The latest articles on DEV Community by gyaaniguy (@gyaaniguy).</description>
    <link>https://dev.to/gyaaniguy</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1038646%2Ff501ee52-0020-490e-94c1-97662218a784.png</url>
      <title>DEV Community: gyaaniguy</title>
      <link>https://dev.to/gyaaniguy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gyaaniguy"/>
    <language>en</language>
    <item>
      <title>Help with tricky mysql query</title>
      <dc:creator>gyaaniguy</dc:creator>
      <pubDate>Fri, 04 Aug 2023 10:20:54 +0000</pubDate>
      <link>https://dev.to/gyaaniguy/help-with-tricky-mysql-query-1d6h</link>
      <guid>https://dev.to/gyaaniguy/help-with-tricky-mysql-query-1d6h</guid>
      <description>&lt;p&gt;Did not work. &lt;del&gt;SOLVED: maybe. will update later.&lt;/del&gt;&lt;/p&gt;

&lt;p&gt;DB structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;section
id 

posts:
id | updated_at | section_id

form_element: 
id | type 

post_form_element_values :
post_id | form_element_id | value 

- Each section can have multiple posts. 
- posts and form_element tables have a many to many relationship.
- form_element: type col can be either 0 OR 1 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Task: Get all posts of a particular section that satisfy the criteria&lt;br&gt;
&lt;code&gt;type = 2 AND f.value &amp;lt; '2023-08-04 02:33:11' OR type=0&lt;/code&gt;&lt;br&gt;
Note: all posts of a section will have the same type value (0 OR 1)&lt;/p&gt;



&lt;p&gt;I wrote this :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT p.id 
FROM posts p
WHERE  p.section_id = 4 and p.id NOT IN (SELECT f.post_id 
                   FROM post_form_element_values f
                            LEFT JOIN form_element v ON v.id = f.form_element_id
                    WHERE (v.type = 2 AND f.value &amp;gt; '2023-08-04'))
ORDER BY p.id
LIMIT 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Works perfectly - 50ms. &lt;br&gt;
But changing "ORDER BY p.id" to "ORDER BY p.updated_at" slows it - 3 seconds &lt;br&gt;
But removing (NOT IN) and keeping "ORDER BY p.updated_at" is still fast.&lt;br&gt;
On its own the subquery is fast - 30ms&lt;/p&gt;

&lt;p&gt;How can I write this differently ? Stuck for days. I tried joins and not exists. this is killing me...&lt;/p&gt;




&lt;p&gt;Explanation of various experiments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ORDER BY &lt;strong&gt;updated_at&lt;/strong&gt; AND 'not in' condition' (slow)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;1,PRIMARY,s,range,section_id_idx,section_id_idx,,9,114764,Using index condition; &lt;strong&gt;Using where; Using filesort&lt;/strong&gt;&lt;br&gt;
2,DEPENDENT SUBQUERY,s3,ref,entry_id_idx,"answer_index_idx,entry_id_idx,form_element_id_idx",func,8,3,Using where&lt;br&gt;
2,DEPENDENT SUBQUERY,s4,eq_ref,PRIMARY,"PRIMARY,tenant_id_idx,form_id_idx,widget_id_idx,privacy_idx,form_element_category_id_form_element_id",.s3.form_element_id,8,1,Using where&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;ORDER BY &lt;strong&gt;id ** AND 'not in' Condition (fast)&lt;br&gt;
1,PRIMARY,s,ref,section_id_idx,section_id_idx,9,const,114764,&lt;/strong&gt;Using where; Using index**&lt;br&gt;
2,DEPENDENT SUBQUERY,s3,ref,"answer_index_idx,entry_id_idx,form_element_id_idx",entry_id_idx,8,func,3,Using where&lt;br&gt;
2,DEPENDENT SUBQUERY,s4,eq_ref,"PRIMARY,tenant_id_idx,form_id_idx,widget_id_idx,privacy_idx,&lt;em&gt;form_element_category_id&lt;/em&gt;_form_element_id",PRIMARY,8,labspadang.s3.form_element_id,1,Using where&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;ORDER BY  &lt;strong&gt;p.updated_at&lt;/strong&gt; AND  'NOT IN' is removed (fast) &lt;br&gt;
1,SIMPLE,s,range,section_id_idx,section_id_idx,9,,114764,&lt;strong&gt;Using index condition; Using filesort&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;help please&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>mysql</category>
      <category>mariadb</category>
    </item>
  </channel>
</rss>
