title: PostgreSQL jsonb indexing for optimized search performance
body: <p>I'm using PostgreSQL 10.1 <strong><em>jsonb</em></strong> data type and designing a JSON document of the following structure:</p>
  
  <pre><code>{
      "guid": "9c36adc1-7fb5-4d5b-83b4-90356a46061a",
      "name": "Angela Barton",
      "is_active": true,
      "company": "Magnafone",
      "address": "178 Howard Place, Gulf, Washington, 702",
      "registered": "2009-11-07T08:53:22 +08:00",
      "latitude": 19.793713,
      "longitude": 86.513373,
      "timestamp": 2001-09-28 01:00:00,
      "tags": [
          "enim",
          "aliquip",
          "qui"
      ]
  }
  </code></pre>
  
  <p>I need to retrieve JSON documents by searching based on <strong>tags</strong> and sorted by <strong>timestamp</strong>.</p>
  
  <p>I have read these documenations and it says <em>jsonb_path_ops</em> offers better performance:</p>
  
  <ol>
  <li><a href="https://www.postgresql.org/docs/current/static/datatype-json.html#JSON-INDEXING" rel="nofollow noreferrer">https://www.postgresql.org/docs/current/static/datatype-json.html#JSON-INDEXING</a></li>
  <li><a href="https://www.postgresql.org/docs/current/static/gin-builtin-opclasses.html" rel="nofollow noreferrer">https://www.postgresql.org/docs/current/static/gin-builtin-opclasses.html</a></li>
  </ol>
  
  <p>To index tags, 1. gave an example:</p>
  
  <pre><code>CREATE INDEX idxgintags ON api USING GIN ((jdoc -&gt; 'tags'));
  </code></pre>
  
  <p>However, the example uses <em>jsonb_ops</em> but I think it is better to use <em>jsonb_path_ops</em>.</p>
  
  <p>Based on the needs for my case, which is to be able to <strong>search based on tags</strong> and <strong>sorted by timestamp</strong>, what is the best way - in terms of <strong>optimized search performance</strong> - to create the indexes? I'd appreciate if an expert could give me the SQL to create the index and some examples to query for data.</p>
  
  <p>Thanks!</p>