<?xml version="1.0" encoding="utf-8" ?>

<rss version="2.0" 
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:admin="http://webns.net/mvcb/"
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
   xmlns:wfw="http://wellformedweb.org/CommentAPI/"
   xmlns:content="http://purl.org/rss/1.0/modules/content/"
   >
<channel>
    
    <title>BostonGIS - database standards</title>
    <link>https://www.bostongis.com/blog/</link>
    <description>Open Source GIS</description>
    <dc:language>en</dc:language>
    <generator>Serendipity 2.6.0 - http://www.s9y.org/</generator>
    <pubDate>Thu, 03 Jan 2008 07:47:27 GMT</pubDate>

    <image>
    <url>https://www.bostongis.com/blog/templates/default/img/s9y_banner_small.png</url>
    <title>RSS: BostonGIS - database standards - Open Source GIS</title>
    <link>https://www.bostongis.com/blog/</link>
    <width>100</width>
    <height>21</height>
</image>

<item>
    <title>Explain Analyze Geometry Relation Operators and Joins Except Where?</title>
    <link>https://www.bostongis.com/blog/index.php?/archives/37-Explain-Analyze-Geometry-Relation-Operators-and-Joins-Except-Where.html</link>
            <category>database standards</category>
            <category>postgis postgresql</category>
    
    <comments>https://www.bostongis.com/blog/index.php?/archives/37-Explain-Analyze-Geometry-Relation-Operators-and-Joins-Except-Where.html#comments</comments>
    <wfw:comment>https://www.bostongis.com/blog/wfwcomment.php?cid=37</wfw:comment>

    <slash:comments>4</slash:comments>
    <wfw:commentRss>https://www.bostongis.com/blog/rss.php?version=2.0&amp;type=comments&amp;cid=37</wfw:commentRss>
    

    <author>nospam@example.com (Regina Obe)</author>
    <content:encoded>
    
&lt;h2&gt;Geometry Operators in Joins vs. WHERE clause&lt;/h2&gt;
&lt;p&gt;I&#039;ve noticed that most people when they do queries in PostGIS (I presume other spatial databases  as well), seem
to put all there geometry relation (intersects, contains etc.) checks in the WHERE clause instead of the FROM clause of their
SQL statements whereas I tend to do the opposite.&lt;/p&gt;

&lt;p&gt;I&#039;ve always wondered if there is a speed advantage of doing it one way or the other so I decided to look at the 2 EXPLAIN ANALYZE plans
in pgAdmin for these two sample queries. &lt;/p&gt;

&lt;div class=&quot;code&quot;&gt;
&lt;pre&gt;&lt;code&gt;
&lt;span class=&quot;syntax0&quot;&gt;&lt;span class=&quot;syntax8&quot;&gt;SELECT&lt;/span&gt; t.town, &lt;span class=&quot;syntax9&quot;&gt;count&lt;/span&gt;(s.gid)&lt;span class=&quot;syntax8&quot;&gt;as&lt;/span&gt; totschools
&lt;span class=&quot;syntax8&quot;&gt;FROM&lt;/span&gt; towns t 
    &lt;span class=&quot;syntax8&quot;&gt;INNER&lt;/span&gt; &lt;span class=&quot;syntax8&quot;&gt;JOIN&lt;/span&gt; 
    schools s &lt;span class=&quot;syntax8&quot;&gt;ON&lt;/span&gt; &lt;span class=&quot;syntax6&quot;&gt;st_intersects&lt;/span&gt;(t.the_geom, s.the_geom)
&lt;span class=&quot;syntax8&quot;&gt;GROUP&lt;/span&gt; &lt;span class=&quot;syntax8&quot;&gt;BY&lt;/span&gt; t.town
&lt;/span&gt;
&lt;/code&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;div class=&quot;code&quot;&gt;
&lt;pre&gt;&lt;code&gt;
&lt;span class=&quot;syntax0&quot;&gt;&lt;span class=&quot;syntax8&quot;&gt;SELECT&lt;/span&gt; t.town, &lt;span class=&quot;syntax9&quot;&gt;count&lt;/span&gt;(s.gid) &lt;span class=&quot;syntax8&quot;&gt;as&lt;/span&gt; totschools
    &lt;span class=&quot;syntax8&quot;&gt;FROM&lt;/span&gt; towns t, schools s 
&lt;span class=&quot;syntax8&quot;&gt;WHERE&lt;/span&gt; &lt;span class=&quot;syntax6&quot;&gt;st_intersects&lt;/span&gt;(t.the_geom, s.the_geom)
&lt;span class=&quot;syntax8&quot;&gt;GROUP&lt;/span&gt; &lt;span class=&quot;syntax8&quot;&gt;BY&lt;/span&gt; t.town
&lt;/span&gt;
&lt;/code&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;As I expected, the plans are identical and look like this.  &lt;/p&gt;
&lt;img src=&quot;/images/blog_clips/schoolstown_plan.png&quot; title=&quot;PgAdmin Schools town plan&quot; /&gt;

&lt;p&gt;I ran for a couple of other types of queries and got the same conclusions. Even for compound statements like the below, the explain analyze plans were identical and the timings if I run for enough iterations come out on average the same.  
&lt;/p&gt;
&lt;div class=&quot;code&quot;&gt;
&lt;pre&gt;&lt;code&gt;
&lt;span class=&quot;syntax0&quot;&gt;&lt;span class=&quot;syntax1&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;Total&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;runtime:&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;90699.236&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;ms&lt;/span&gt;
&lt;span class=&quot;syntax8&quot;&gt;SELECT&lt;/span&gt; t.town, &lt;span class=&quot;syntax9&quot;&gt;count&lt;/span&gt;(s.gid) &lt;span class=&quot;syntax8&quot;&gt;as&lt;/span&gt; totschools
&lt;span class=&quot;syntax8&quot;&gt;FROM&lt;/span&gt; towns t &lt;span class=&quot;syntax8&quot;&gt;INNER&lt;/span&gt; &lt;span class=&quot;syntax8&quot;&gt;JOIN&lt;/span&gt; schools s &lt;span class=&quot;syntax8&quot;&gt;ON&lt;/span&gt; (&lt;span class=&quot;syntax6&quot;&gt;st_intersects&lt;/span&gt;(t.the_geom, s.the_geom) &lt;span class=&quot;syntax8&quot;&gt;AND&lt;/span&gt; s.grades &lt;span class=&quot;syntax8&quot;&gt;LIKE&lt;/span&gt; &lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;%K%&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;)
&lt;span class=&quot;syntax8&quot;&gt;GROUP&lt;/span&gt; &lt;span class=&quot;syntax8&quot;&gt;BY&lt;/span&gt; t.town

&lt;span class=&quot;syntax1&quot;&gt;/*&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;*HashAggregate&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;(cost=4459.01..4463.40&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;rows=351&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;width=15)&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;(actual&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;time=90698.767..90698.995&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;rows=328&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;loops=1)&lt;/span&gt;
&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;Nested&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;Loop&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;(cost=0.00..4332.89&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;rows=25224&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;width=15)&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;(actual&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;time=63.797..90681.218&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;rows=1496&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;loops=1)&lt;/span&gt;
&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;Join&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;Filter:&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;_st_intersects(t.the_geom,&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;s.the_geom)&lt;/span&gt;
&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;Seq&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;Scan&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;on&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;towns&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;(cost=0.00..131.41&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;rows=1241&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;width=8242)&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;(actual&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;time=0.010..1.966&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;rows=1241&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;loops=1)&lt;/span&gt;
&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;Index&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;Scan&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;using&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;idx_schools_the_geom2&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;on&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;schools&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;(cost=0.00..3.37&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;rows=1&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;width=29)&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;(actual&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;time=25.623..66.418&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;rows=2&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;loops=1241)&lt;/span&gt;
&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;Index&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;Cond:&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;(t.the_geom&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;s.the_geom)&lt;/span&gt;
&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;Filter:&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;(((s.grades)::text&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;~~&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;&#039;%K%&#039;::text)&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;AND&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;(t.the_geom&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;s.the_geom))&lt;/span&gt;
&lt;span class=&quot;syntax1&quot;&gt;Total&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;runtime:&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;90699.236&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;ms&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;*/&lt;/span&gt;
&lt;/span&gt;
&lt;/code&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;div class=&quot;code&quot;&gt;
&lt;pre&gt;&lt;code&gt;
&lt;span class=&quot;syntax0&quot;&gt;&lt;span class=&quot;syntax1&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;Total&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;runtime:&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;92633.716&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;ms&lt;/span&gt;
&lt;span class=&quot;syntax8&quot;&gt;SELECT&lt;/span&gt; t.town, &lt;span class=&quot;syntax9&quot;&gt;count&lt;/span&gt;(s.gid) &lt;span class=&quot;syntax8&quot;&gt;as&lt;/span&gt; totschools
&lt;span class=&quot;syntax8&quot;&gt;FROM&lt;/span&gt; towns t , schools s 
&lt;span class=&quot;syntax8&quot;&gt;WHERE&lt;/span&gt; (&lt;span class=&quot;syntax6&quot;&gt;st_intersects&lt;/span&gt;(t.the_geom, s.the_geom) &lt;span class=&quot;syntax8&quot;&gt;AND&lt;/span&gt; s.grades &lt;span class=&quot;syntax8&quot;&gt;LIKE&lt;/span&gt; &lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;%K%&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;)
&lt;span class=&quot;syntax8&quot;&gt;GROUP&lt;/span&gt; &lt;span class=&quot;syntax8&quot;&gt;BY&lt;/span&gt; t.town

&lt;span class=&quot;syntax1&quot;&gt;/*
HashAggregate  (cost=524.90..529.29 rows=351 width=16)
  -&amp;gt;  Nested Loop  (cost=0.00..517.76 rows=1429 width=16)
        Join Filter: _st_intersects(t.the_geom, s.the_geom)
        -&amp;gt;  Seq Scan on towns t  (cost=0.00..25.51 rows=351 width=25602)
        -&amp;gt;  Index Scan using idx_schools_the_geom2 on schools s  (cost=0.00..1.39 rows=1 width=29)
              Index Cond: (t.the_geom &amp;amp;&amp;amp; s.the_geom)
              Filter: (((s.grades)::text ~~ &#039;%K%&#039;::text) AND (t.the_geom &amp;amp;&amp;amp; s.the_geom))
*/&lt;/span&gt;
&lt;/span&gt;
&lt;/code&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;b&gt;Visual plan for both&lt;/b&gt; &lt;br /&gt;
&lt;img src=&quot;/images/blog_clips/schoolstown_kg_innerplan.png&quot; title=&quot;PgAdmin inner plan&quot; /&gt;

&lt;p&gt;So the question is why is there a preference for putting these things in the WHERE and why do I prefer JOIN?&lt;/p&gt;
&lt;p&gt;If you think about it, JOIN is kind of a weird concept that appeals mostly to database geeks - whereas WHERE is something most people deal with every day.  
WHERE is more intuitive; Also let us not forget the nonconformist empire of Oracle and how in the olden days of Oracle, JOINS were
done in the WHERE clause with things like =+ = += etc., to do LEFT INNER RIGHT FULL JOINS and I suspect a lot of Oracle Database users still do that even though it violates the ANSI SQL Standard.
&lt;/p&gt;
&lt;p&gt;Now why do I prefer JOIN over WHERE for this kind of thing
&lt;/p&gt;&lt;ul&gt;
	&lt;li&gt;An INNER JOIN can be flipped over to a LEFT or RIGHT JOIN at the drop of a hat (give me all in set A even if there is no matching record in set B etc.) . There is no equivalent concept for WHERE except in Oracle.  In the case above - yes should a town not have schools I probably want to know that and would change to a LEFT JOIN instead of having it left out of my result as would happen if I had the clause in the WHERE or INNER JOIN.&lt;/li&gt;
	&lt;li&gt;If I have at least one JOIN condition for each table, I usually don&#039;t make the mistake of doing an accidental cartesian product by forgetting a WHERE condition.  This is of course just me.  Others may not have this problem.&lt;/li&gt;	
&lt;/ul&gt;


&lt;h2&gt;Why ever ask WHERE?&lt;/h2&gt;
&lt;p&gt;Now while I do tend to make sure that I have at least one JOIN condition for each of my tables, I also use WHERE? If you can do LEFT RIGHT INNER
with JOINs and WHERE can only simulate INNER JOINS, why is WHERE ever important?  &lt;/p&gt;

&lt;p&gt;Well I can think of several cases where I would use WHERE in conjunction with
JOIN, some cases where I arbitrarily choose JOIN and no WHERE out of habit, and some where I just have a WHERE such as when I have a VIEW so I put the JOIN in the view and a where in my query against the view or I have a single table or my audience doesn&#039;t understand the concept of JOIN so its easier to just use WHERE.  Below is an example where WHERE is a useful thing and can&#039;t be replaced with just a JOIN.&lt;/p&gt;

&lt;b&gt;EXCEPTION Queries: Give me a list of all towns that have no kindergartens&lt;/b&gt;
&lt;div class=&quot;code&quot;&gt;
&lt;pre&gt;&lt;code&gt;
&lt;span class=&quot;syntax0&quot;&gt;&lt;span class=&quot;syntax1&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;Total&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;runtime:&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;90916.939&lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax1&quot;&gt;ms&lt;/span&gt;
&lt;span class=&quot;syntax8&quot;&gt;SELECT&lt;/span&gt; t.town
&lt;span class=&quot;syntax8&quot;&gt;FROM&lt;/span&gt; towns t 
    &lt;span class=&quot;syntax9&quot;&gt;LEFT&lt;/span&gt; &lt;span class=&quot;syntax8&quot;&gt;JOIN&lt;/span&gt; 
        schools s &lt;span class=&quot;syntax8&quot;&gt;ON&lt;/span&gt; 
(&lt;span class=&quot;syntax6&quot;&gt;st_intersects&lt;/span&gt;(t.the_geom, s.the_geom) 
	&lt;span class=&quot;syntax8&quot;&gt;AND&lt;/span&gt; s.grades &lt;span class=&quot;syntax8&quot;&gt;LIKE&lt;/span&gt; &lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;%K%&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;)
&lt;span class=&quot;syntax8&quot;&gt;WHERE&lt;/span&gt; s.gid &lt;span class=&quot;syntax8&quot;&gt;IS&lt;/span&gt; &lt;span class=&quot;syntax8&quot;&gt;NULL&lt;/span&gt;
&lt;span class=&quot;syntax8&quot;&gt;ORDER BY&lt;/span&gt; t.town
&lt;/span&gt;

&lt;span class=&quot;syntax1&quot;&gt;Sort  (cost=517.77..517.77 rows=1 width=12) (actual time=17839.682..17839.687 rows=23 loops=1)
  Sort Key: t.town
  -&amp;gt;  Nested Loop Left Join  (cost=0.00..517.76 rows=1 width=12) (actual time=37.866..17839.617 rows=23 loops=1)
        Join Filter: _st_intersects(t.the_geom, s.the_geom)
        Filter: (s.gid IS NULL)
        -&amp;gt;  Seq Scan on towns t  (cost=0.00..25.51 rows=351 width=25602) (actual time=0.006..0.303 rows=351 loops=1)
        -&amp;gt;  Index Scan using idx_schools_the_geom2 on schools s  (cost=0.00..1.39 rows=1 width=29) (actual time=6.792..29.123 rows=7 loops=351)
              Index Cond: (t.the_geom &amp;amp;&amp;amp; s.the_geom)
              Filter: (((s.grades)::text ~~ &#039;%K%&#039;::text) AND (t.the_geom &amp;amp;&amp;amp; s.the_geom))
Total runtime: 17839.764 ms
&lt;/span&gt;

&lt;/code&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;img src=&quot;/images/blog_clips/schoolstown_kg_leftplan.png&quot; title=&quot;PgAdmin Left Schools Kindergarten Plan&quot; /&gt;
&lt;br /&gt;
&lt;p&gt;It is really quite hard to answer the above question without a WHERE and using a NOT IN(subselect) or EXCEPT clause to avoid a JOIN is in general slower in DBMSs I have tried. Not in all cases though.  
As shown later below my NOT IN for this particular case beats out the LEFT 2 out of 3 times.  This could have more to do with the fact that the planner has an easier time optimizing geometric INNER JOINS than LEFT JOINS. 
&lt;/p&gt;

What the above query is basically doing is 
&lt;ul&gt;&lt;li&gt;Match up the towns with schools with kindergartens that fall in the town region and if no match, then create a placeholder (filled with all nulls where we would otherwise have school data) so that we always have at least one school record for each town in our result. &lt;br /&gt;
	I like to think of this as creating an imaginary kindergarten for towns that can&#039;t afford a real one so that no town is left behind. Kind of like &lt;a href=&quot;http://en.wikipedia.org/wiki/Silver_Line_%28MBTA%29&quot; target=&quot;_blank&quot;&gt;Boston&#039;s Silver Line affectionately called &lt;i&gt;The Silver Lie&lt;/i&gt;&lt;/a&gt;&lt;br /&gt;In a nutshell that is what a LEFT JOIN is.  It makes the impossible possible by saying create a place holder in the second data set where there is no match.&lt;/li&gt;
	&lt;li&gt;Now give me only a list of towns that have a non-existent kindergarten.
&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt; 
&lt;/p&gt;&lt;p&gt;Now some may ask -&lt;br /&gt;
&lt;b&gt;Q: &lt;/b&gt; Why can&#039;t you put the &lt;i&gt;s.gid IS NULL&lt;/i&gt; in the LEFT JOIN clause and don&#039;t ask WHERE? &lt;br /&gt;
&lt;b&gt;A: &lt;/b&gt; Because the LEFT JOIN is what creates the &lt;b&gt;concept&lt;/b&gt; of a nonexistent kindergarten school so if you ask
for such a thing before the &lt;b&gt;concept&lt;/b&gt; of such a thing exists, 
you get all towns because there exists no real school that is imaginary. It doesn&#039;t take the planner long to figure out that there is no such school and to create a virtual school placeholder for all towns. 
In fact if you run such a query, the planner will return all the towns to you and very quickly in fact because it completely ignores the 
costly intersect check because it needs to do very little analysis to determine that doing an expensive intersect check is more costly than taking the obvious that a primary key (s.gid can not have nulls if it actually exists or a real school is never imaginary).&lt;/p&gt;
&lt;br /&gt;&lt;br /&gt;

&lt;p&gt;Its kind of strange that in most DBMSs I have tried, doing a left is in general faster than doing a NOT IN or EXCEPT.  My guess for this reason is 
a LEFT takes in general less memory and sorting power to process.  This varies depending on datasets and the properties of your dataset. &lt;/p&gt;

&lt;p&gt;Think about the case of if the two sets of geometries intersect I can immediately throw out those results because looking forward I know I really don&#039;t care about towns that have kindergartens so as soon as I see such a thing - I throw it away. 
So while I need to process it, I immediately throw this information away. In fact think about it - I never asked what schools it has - so as soon as I see a town with a kindergarten - I could care less about it.  
I never have to consider this town again or join it or order it with my other towns. 
I do not need to remember it. 
&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;


&lt;p&gt;
If I do a NOT IN or EXCEPT I first have to ask which towns have kindergartens and then which towns are not in the set of towns that have kindergartens.  
MORE MEMORY and processor needed for sorting in general because I need to know the towns that have kindergartens to compare with my full town set to know the towns that don&#039;t have kindergartens.  Well its not quite that simple for NOT IN and EXCEPT, but almost because the planner sees these as separate distinct questions instead of in the 
case with the LEFT WHERE where it perceives that as one question.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Below is the same question - asked with a NOT IN.  
Note: We are asking who is in the list (our inner question) and who is not in the list that we just generated.&lt;/b&gt;&lt;/p&gt;
&lt;div class=&quot;code&quot;&gt;
&lt;pre&gt;&lt;code&gt;
&lt;span class=&quot;syntax0&quot;&gt;&lt;span class=&quot;syntax8&quot;&gt;SELECT&lt;/span&gt; t.town
&lt;span class=&quot;syntax8&quot;&gt;FROM&lt;/span&gt; towns t 
&lt;span class=&quot;syntax8&quot;&gt;WHERE&lt;/span&gt; t.gid 
    &lt;span class=&quot;syntax8&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;syntax8&quot;&gt;IN&lt;/span&gt;(&lt;span class=&quot;syntax8&quot;&gt;SELECT&lt;/span&gt; t.gid 
            &lt;span class=&quot;syntax8&quot;&gt;FROM&lt;/span&gt; towns t, schools s 
        &lt;span class=&quot;syntax8&quot;&gt;WHERE&lt;/span&gt; (st_intersects(t.the_geom, s.the_geom) &lt;span class=&quot;syntax8&quot;&gt;AND&lt;/span&gt; s.grades &lt;span class=&quot;syntax8&quot;&gt;LIKE&lt;/span&gt; &lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;%K%&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;))
&lt;span class=&quot;syntax8&quot;&gt;ORDER&lt;/span&gt; &lt;span class=&quot;syntax8&quot;&gt;BY&lt;/span&gt; t.town
&lt;/span&gt;

&lt;span class=&quot;syntax1&quot;&gt;
/** Sort  (cost=554.28..554.72 rows=176 width=12) (actual time=17804.008..17804.014 rows=23 loops=1)
  Sort Key: town
  -&amp;gt;  Seq Scan on towns t  (cost=521.33..547.72 rows=176 width=12) (actual time=17803.827..17803.977 rows=23 loops=1)
        Filter: (NOT (hashed subplan))
        SubPlan
          -&amp;gt;  Nested Loop  (cost=0.00..517.76 rows=1429 width=4) (actual time=7.226..17800.803 rows=1496 loops=1)
                Join Filter: _st_intersects(t.the_geom, s.the_geom)
                -&amp;gt;  Seq Scan on towns t  (cost=0.00..25.51 rows=351 width=25594) (actual time=0.001..0.290 rows=351 loops=1)
                -&amp;gt;  Index Scan using idx_schools_the_geom2 on schools s  (cost=0.00..1.39 rows=1 width=25) (actual time=6.856..29.051 rows=7 loops=351)
                      Index Cond: (t.the_geom &amp;amp;&amp;amp; s.the_geom)
                      Filter: (((s.grades)::text ~~ &#039;%K%&#039;::text) AND (t.the_geom &amp;amp;&amp;amp; s.the_geom))
Total runtime: 17804.101 ms  ***/
&lt;/span&gt;
&lt;/code&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;img src=&quot;/images/blog_clips/schoolstown_kg_notinclause.png&quot; title=&quot;PgAdmin Not In Plan&quot; /&gt;
&lt;br /&gt;
&lt;b&gt;Same question with EXCEPT - return all towns except those that have kindergartens&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;
&lt;div class=&quot;code&quot;&gt;
&lt;pre&gt;	&lt;code&gt;
&lt;span class=&quot;syntax0&quot;&gt;&lt;span class=&quot;syntax8&quot;&gt;SELECT&lt;/span&gt; t.town
    &lt;span class=&quot;syntax8&quot;&gt;FROM&lt;/span&gt; towns t
EXCEPT
&lt;span class=&quot;syntax8&quot;&gt;SELECT&lt;/span&gt; t.town
    &lt;span class=&quot;syntax8&quot;&gt;FROM&lt;/span&gt; towns t ,schools s 
    &lt;span class=&quot;syntax8&quot;&gt;WHERE&lt;/span&gt; (st_intersects(t.the_geom, s.the_geom) 
    &lt;span class=&quot;syntax8&quot;&gt;AND&lt;/span&gt; s.grades &lt;span class=&quot;syntax8&quot;&gt;LIKE&lt;/span&gt; &lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;%K%&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;)
&lt;span class=&quot;syntax8&quot;&gt;ORDER&lt;/span&gt; &lt;span class=&quot;syntax8&quot;&gt;BY&lt;/span&gt; &lt;span class=&quot;syntax5&quot;&gt;1&lt;/span&gt;

&lt;span class=&quot;syntax1&quot;&gt;/**SetOp Except  (cost=657.16..666.06 rows=178 width=12) (actual time=20962.974..20964.141 rows=23 loops=1)
  -&amp;gt;  Sort  (cost=657.16..661.61 rows=1780 width=12) (actual time=20962.956..20963.398 rows=1847 loops=1)
        Sort Key: town
        -&amp;gt;  Append  (cost=0.00..561.07 rows=1780 width=12) (actual time=0.007..20958.763 rows=1847 loops=1)
              -&amp;gt;  Subquery Scan *SELECT* 1  (cost=0.00..29.02 rows=351 width=12) (actual time=0.006..0.364 rows=351 loops=1)
                    -&amp;gt;  Seq Scan on towns t  (cost=0.00..25.51 rows=351 width=12) (actual time=0.005..0.178 rows=351 loops=1)
              -&amp;gt;  Subquery Scan *SELECT* 2  (cost=0.00..532.05 rows=1429 width=12) (actual time=7.062..20957.331 rows=1496 loops=1)
                    -&amp;gt;  Nested Loop  (cost=0.00..517.76 rows=1429 width=12) (actual time=7.061..20955.576 rows=1496 loops=1)
                          Join Filter: _st_intersects(t.the_geom, s.the_geom)
                          -&amp;gt;  Seq Scan on towns t  (cost=0.00..25.51 rows=351 width=25602) (actual time=0.001..0.293 rows=351 loops=1)
                          -&amp;gt;  Index Scan using idx_schools_the_geom2 on schools s  (cost=0.00..1.39 rows=1 width=25) (actual time=6.721..28.799 rows=7 loops=351)
                                Index Cond: (t.the_geom &amp;amp;&amp;amp; s.the_geom)
                                Filter: (((s.grades)::text ~~ &#039;%K%&#039;::text) AND (t.the_geom &amp;amp;&amp;amp; s.the_geom))
Total runtime: 20964.237 ms **/&lt;/span&gt;
&lt;/span&gt;
		&lt;/code&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;img src=&quot;/images/blog_clips/schoolstown_kg_exceptplan.png&quot; title=&quot;PgAdmin Except&quot; /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;p&gt;One reason why it takes less memory to do a LEFT JOIN is what makes database programming a little harder to understand than standard procedural logic.  Remember I stressed the idea of &lt;b&gt;concepts&lt;/b&gt; verses &lt;b&gt;reality&lt;/b&gt;.  Sure the idea of a school that doesn&#039;t actually exist is kind of silly but its a useful model and allows us to do one
very important thing - &lt;b&gt;IRRADICATE INCONVENIENT EXCEPTIONS&lt;/b&gt;.  All my towns now have kindergartens (sort of) so I am no longer asking a question I have no data for.  The fact that some of these kindergartens are imaginary and that I am now on the hunt for towns with imaginary kindergartens is inconsequential. 
I have irradicated this inconvenient exception that forces me to ask, Which towns have kindergartens, a question I could care less about.&lt;/p&gt;

&lt;p&gt;
Although JOINS happen conceptually before WHERE,  in reality they don&#039;t need to as long as the planner can guarantee the process of solving things out of order mimicks the conceptual model. In reality they never need to happen at all if reality can mimick concept.  
For example if you had in your WHERE clause &lt;code&gt;towns.town LIKE &#039;ZZ%&#039;&lt;/code&gt; and you have an index on town,  your JOIN would most likely never happen because the database planner would run the WHERE part first since its less costly and would not affect the results (violate the conceptual model).  
It would then quickly realize no such Town exists and therefore the result is the empty set and doing a join will not change this result.
&lt;/p&gt;
&lt;p&gt;
Its actually a wonderful hack that you can state a problem, see annoying exceptions to the rule and quickly concoct conceptual models that destroy these nasty exceptions so you can treat everything the same.
&lt;/p&gt; 
    </content:encoded>

    <pubDate>Sun, 28 Oct 2007 04:18:00 -0400</pubDate>
    <guid isPermaLink="false">https://www.bostongis.com/blog/index.php?/archives/37-guid.html</guid>
    
</item>
<item>
    <title>Database Information Schema Catalog, NULLS, and Code Generation</title>
    <link>https://www.bostongis.com/blog/index.php?/archives/36-Database-Information-Schema-Catalog,-NULLS,-and-Code-Generation.html</link>
            <category>database standards</category>
            <category>postgis postgresql</category>
    
    <comments>https://www.bostongis.com/blog/index.php?/archives/36-Database-Information-Schema-Catalog,-NULLS,-and-Code-Generation.html#comments</comments>
    <wfw:comment>https://www.bostongis.com/blog/wfwcomment.php?cid=36</wfw:comment>

    <slash:comments>0</slash:comments>
    <wfw:commentRss>https://www.bostongis.com/blog/rss.php?version=2.0&amp;type=comments&amp;cid=36</wfw:commentRss>
    

    <author>nospam@example.com (Regina Obe)</author>
    <content:encoded>
    
&lt;p&gt;I love the idea of code generation without using IDEs just because I know if my IDEs of choice are not present
at a particular moment, I&#039;ve got another swiss army knife to rely on.  
I particularly like SQL code generation schemes that rely on a database to do it.  When you are trying to
generate SQL code, you often need the metadata of your table structures to do so and using the database to get that info is often the easiest approach.
&lt;/p&gt;
&lt;p&gt;This post is in the spirit of Hubert Lubaczewski&#039;s recent PostgreSQL post &lt;a href=&quot;http://www.depesz.com/index.php/2007/10/19/grantall/&quot; target=&quot;_blank&quot;&gt;grant XXX on * ?&lt;/a&gt; which details how to grant access to tables in PostgreSQL using some SQL code generation tricks.  As it turns out I had a similar situation I had to deal with recently, but involved updating fields to null.
&lt;/p&gt;
&lt;h2&gt;Empty String verses Null&lt;/h2&gt;
&lt;p&gt;Now there have been long debates about when to use Null vs. Empty string and the various Gotchas involved.  If you want to know about these, this series on Nulls
&lt;a href=&quot;http://sqlblog.com/blogs/hugo_kornelis/archive/2007/09/30/what-if-null-if-null-is-null-null-null-is-null.aspx&quot; target=&quot;_blank&quot;&gt;What if null if null is null null null is null?&lt;/a&gt;
by Hugo Kornelis is a pretty good one.  Hugo is predominately a Microsoft SQL Server blogger, but for the most part how null
is handled is pretty much the same across most pseudo ANSI standard relational databases.  So regardless of your database poisons of choice, his comments have
equal merit.
&lt;/p&gt;
&lt;p&gt;
I generally prefer the use of Null instead of empty string, all gotchas aside, mostly for philosophical reasons;  
I believe that the absence of data should be a black hole and that is what NULL is. When you do a lot of statistical and financial reporting as we do, its immensely useful, but you have to be cognizant of the difference between 0, empty string and NULL and that it is used consistently within your database.  
There is one other reason I prefer NULL over using an empty string and that is that
NULL can be cast to any data type because it represents the existence of nothing where as an empty string can not because an empty string is a string.  This gets me to my particular dilemma.
&lt;/p&gt;

&lt;h2&gt;Updating Empty String to NULL&lt;/h2&gt;
&lt;p&gt;Often times when I get property parcel data they send it to me as all varchar or some such thing and they are always changing
the field names on me (the evils of DBF/ESRI shape as a transport mechanism) and I then have to massage this data into my superbly structured tables where a number is a number and can be tabulated without casting.  Now take
the case if you have say land property&#039;s assessed value or some other numeric field that comes as varchar and you need to stuff it into an integer or float (double precision) field and a lot of these fields come thru as empty string.
&lt;/p&gt;
&lt;p&gt;
You get an error like this - ERROR:  invalid input syntax for double precision: &amp;quot;&amp;quot; in PostgreSQL (or SQL Server) when you do something like. &lt;br /&gt;
&lt;code&gt;SELECT CAST(&#039;&#039; AS float)&lt;/code&gt;
&lt;br /&gt;
&lt;/p&gt;&lt;p&gt;
But if you do something like &lt;code&gt;SELECT CAST(NULL as float)&lt;/code&gt; &lt;br /&gt;&lt;br /&gt;that works just fine,  
because all kinds of data can have black holes.  NULL is the universal thing that is a party of all data sets.  
It is equivalent to the empty set in set theory.
&lt;/p&gt;
&lt;br /&gt;
So the way I get around this unpleasantry is to set all varchar fields 
that are empty to null before I try to insert it into my final table structure.  You can imagine this gets pretty repetitive if you have to do this for say
20 fields.  So here is my trick to generate the SQL to do this in PostgreSQL.
&lt;div class=&quot;code&quot;&gt;
&lt;code&gt;
&lt;/code&gt;&lt;pre&gt;&lt;span class=&quot;syntax0&quot;&gt;&lt;span class=&quot;syntax8&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;UPDATE&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt; &lt;span class=&quot;syntax18&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;syntax18&quot;&gt;|&lt;/span&gt; table_name &lt;span class=&quot;syntax18&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;syntax18&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;SET&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt; &lt;span class=&quot;syntax18&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;syntax18&quot;&gt;|&lt;/span&gt; column_name &lt;span class=&quot;syntax18&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;syntax18&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;WHERE&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt; &lt;span class=&quot;syntax18&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;syntax18&quot;&gt;|&lt;/span&gt; column_name &lt;span class=&quot;syntax18&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;syntax18&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;   &lt;span class=&quot;syntax8&quot;&gt;As&lt;/span&gt; sqlupdate
&lt;span class=&quot;syntax8&quot;&gt;FROM&lt;/span&gt; information_schema.columns 
&lt;span class=&quot;syntax8&quot;&gt;WHERE&lt;/span&gt; table_name &lt;span class=&quot;syntax18&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;sometable&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt; &lt;span class=&quot;syntax8&quot;&gt;AND&lt;/span&gt; data_type &lt;span class=&quot;syntax8&quot;&gt;LIKE&lt;/span&gt; &lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;%char%&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;
&lt;/span&gt;
&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The above will return a row for each column in your table that is a character varying or char field and will contain the update statement to update all empty strings in that column to NULL.  If you want to go one better,
you can create a custom aggregate for strings (as I mentioned in &lt;a href=&quot;https://www.bostongis.com/blog/index.php?/archives/2007/10/02.html&quot; target=&quot;_blank&quot;&gt;More generate series&lt;/a&gt;) and use it to get a single row containing all your update statements.
&lt;/p&gt;
&lt;p&gt;Note that this same trick works in other databases that support the ISO-SQL:1999+ information_schema such as (SQL Server 2005, SQL Server 2000, MySQL 5, PostgreSQL 7.4 +) - (sadly Oracle apparently doesn&#039;t support information_schema - perhaps someday. :)).  
&lt;/p&gt;
In SQL Server 2000/2005 you would replace the || with + so your code would look like
&lt;div class=&quot;code&quot;&gt;
&lt;code&gt;
&lt;/code&gt;&lt;pre&gt;&lt;span class=&quot;syntax0&quot;&gt;&lt;span class=&quot;syntax8&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;UPDATE&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt; &lt;span class=&quot;syntax18&quot;&gt;+&lt;/span&gt; table_name &lt;span class=&quot;syntax18&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;SET&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt; &lt;span class=&quot;syntax18&quot;&gt;+&lt;/span&gt; column_name &lt;span class=&quot;syntax18&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;WHERE&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt; &lt;span class=&quot;syntax18&quot;&gt;+&lt;/span&gt; column_name &lt;span class=&quot;syntax18&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;   &lt;span class=&quot;syntax8&quot;&gt;As&lt;/span&gt; sqlupdate
&lt;span class=&quot;syntax8&quot;&gt;FROM&lt;/span&gt; information_schema.columns 
&lt;span class=&quot;syntax8&quot;&gt;WHERE&lt;/span&gt; table_name &lt;span class=&quot;syntax18&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;sometable&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt; &lt;span class=&quot;syntax8&quot;&gt;AND&lt;/span&gt; data_type &lt;span class=&quot;syntax8&quot;&gt;LIKE&lt;/span&gt; &lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;%char%&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;
&lt;/span&gt;
&lt;/pre&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;/div&gt;
In MySQL you would have to use the CONCAT function if you are not in ANSI mode.  For MySQL 5+ you can do &lt;code&gt;set sql_mode = &#039;ANSI&#039;;&lt;/code&gt; and use the standard ||. 
&lt;br /&gt;&lt;br /&gt;
&lt;p&gt;
&lt;/p&gt;&lt;p&gt;If you want to have the above return a single row in SQL Server 2005, similarly you would create an aggregate function for strings
as &lt;a href=&quot;http://technet.microsoft.com/en-us/library/ms131056.aspx&quot; target=&quot;_blank&quot;&gt;described here&lt;/a&gt; - granted a bit more involved than doing the same in PostgreSQL.  
Or use SQL Server&#039;s XPath syntax supported in SQL Server 2005 as &lt;a href=&quot;http://weblogs.asp.net/dwahlin/archive/2006/09/26/Using-SQL-Server-2005-CLR-Features.aspx&quot; target=&quot;_blank&quot;&gt;described here&lt;/a&gt;.
&lt;/p&gt;

&lt;p&gt;Note that if you are making heavy use of database schemas,
then you will need to qualify your tables with the schema name and do a where on the table_schema as well.
&lt;/p&gt;

&lt;div class=&quot;code&quot;&gt;
&lt;code&gt;
&lt;/code&gt;&lt;pre&gt;&lt;span class=&quot;syntax0&quot;&gt;&lt;span class=&quot;syntax8&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;UPDATE&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt; &lt;span class=&quot;syntax18&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;syntax18&quot;&gt;|&lt;/span&gt; table_schema &lt;span class=&quot;syntax18&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;syntax18&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt; &lt;span class=&quot;syntax18&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;syntax18&quot;&gt;|&lt;/span&gt; table_name &lt;span class=&quot;syntax18&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;syntax18&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;SET&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt; &lt;span class=&quot;syntax18&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;syntax18&quot;&gt;|&lt;/span&gt; column_name &lt;span class=&quot;syntax18&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;syntax18&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;WHERE&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt; &lt;span class=&quot;syntax18&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;syntax18&quot;&gt;|&lt;/span&gt; column_name &lt;span class=&quot;syntax18&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;syntax18&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;   &lt;span class=&quot;syntax8&quot;&gt;As&lt;/span&gt; sqlupdate
&lt;span class=&quot;syntax8&quot;&gt;FROM&lt;/span&gt; information_schema.columns 
&lt;span class=&quot;syntax8&quot;&gt;WHERE&lt;/span&gt; table_name &lt;span class=&quot;syntax18&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;sometable&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt; &lt;span class=&quot;syntax8&quot;&gt;AND&lt;/span&gt; data_type &lt;span class=&quot;syntax8&quot;&gt;LIKE&lt;/span&gt; &lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;%char%&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt; &lt;span class=&quot;syntax8&quot;&gt;AND&lt;/span&gt; table_schema &lt;span class=&quot;syntax18&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;assessing&lt;/span&gt;&lt;span class=&quot;syntax13&quot;&gt;&#039;&lt;/span&gt;;
&lt;/span&gt;
&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;Explore the INFORMATION_SCHEMA&lt;/h2&gt;
&lt;p&gt;The INFORMATION_SCHEMA is chuck-full of all sorts of useful metadata about your database objects. While some tables may not exist
in some database management systems (DBMS) (e.g. PostgreSQL has a table information_schema.sequences (defined in SQL:2003 standard which you won&#039;t find in MYSQL and SQL Server because those databases don&#039;t have sequence objects),
for the tables that exist, the tables are consistently named across all DBMS&#039;s that support them and so are the field names.
&lt;/p&gt;
&lt;p&gt;There are 3 tables (views) I find most useful in the information schema. These are all available in the DBMS&#039;s I mentioned, and those are&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;INFORMATION_SCHEMA.TABLES - contains listing of all tables and views in a database&lt;/li&gt;
	&lt;li&gt;INFORMATION_SCHEMA.COLUMNS - contains listing of all columns in a database, the table they belong to, the data type, length, ordinal position in table&lt;/li&gt;
	&lt;li&gt;INFORMATION_SCHEMA.VIEWS - contains names of views and the SQL that defines the view&lt;/li&gt;
	&lt;li&gt;These ones I haven&#039;t explored but look useful for doing security audits on your tables - INFORMATION_SCHEMA.TABLE_PRIVILEGES, INFORMATION_SCHEMA.COLUMN_PRIVILEGES&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Note - for the most part if not in all cases, the INFORMATION_SCHEMA set of information are pretty much implemented as views on top of the proprietary DBMS system tables.  The reasons I find most compelling to use them instead of using the direct system tables are the following&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;It is often easier to navigate for basic metadata than trying to navigate the DBMS system tables.  I have found it very rare to need information that I couldn&#039;t find in the information_schema and needed to resort to system tables.&lt;/li&gt;
&lt;li&gt;Since it is purely there for informational purposes and is a standard, you don&#039;t have to worry too much about it changing as you would
a system table - whose purpose is more for direct management of database internals.&lt;/li&gt;
&lt;li&gt;Since it is a standard, for the databases that comply with the standard you can feel a bit more at home, since there is one less thing you need to know about to migrate. Thus less feeling of Vendor Lock-In.&lt;/li&gt;
&lt;/ul&gt;
	
 
    </content:encoded>

    <pubDate>Sat, 20 Oct 2007 00:00:00 -0400</pubDate>
    <guid isPermaLink="false">https://www.bostongis.com/blog/index.php?/archives/36-guid.html</guid>
    
</item>

</channel>
</rss>
