<?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: Tharun Parepalli</title>
    <description>The latest articles on DEV Community by Tharun Parepalli (@tharun_parepalli).</description>
    <link>https://dev.to/tharun_parepalli</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3947968%2Fd2c4e5d8-c302-41d6-b390-85e2cd46d549.jpg</url>
      <title>DEV Community: Tharun Parepalli</title>
      <link>https://dev.to/tharun_parepalli</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tharun_parepalli"/>
    <language>en</language>
    <item>
      <title>What Happens When You Ask AI “Where’s My Order?” (MCP Explained)</title>
      <dc:creator>Tharun Parepalli</dc:creator>
      <pubDate>Sun, 15 Nov 2026 11:00:22 +0000</pubDate>
      <link>https://dev.to/tharun_parepalli/what-happens-when-you-ask-ai-wheres-my-order-mcp-explained-527e</link>
      <guid>https://dev.to/tharun_parepalli/what-happens-when-you-ask-ai-wheres-my-order-mcp-explained-527e</guid>
      <description>&lt;h2&gt;
  
  
  &lt;em&gt;Where’s my order #1042?&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;It’s a simple question for a store chatbot. The order number is right there, and a short answer will do. But the one piece of information the customer actually wants is missing from the message: the current shipping status.&lt;/p&gt;

&lt;p&gt;The store already has that information. What the chat application needs is a way to find the right operation, pass it order 1042, and use the result in its reply. That’s where the Model Context Protocol (MCP) comes in.&lt;/p&gt;

&lt;p&gt;Let’s follow one order lookup from the chat window to the backend and back. Then we’ll look at how MCP fits alongside a REST API and what happens when the customer asks a follow-up.&lt;/p&gt;

&lt;h2&gt;
  
  
  The parts of an MCP integration
&lt;/h2&gt;

&lt;p&gt;Four pieces take part in the order lookup. The model and the MCP client have different jobs, even though the AI host coordinates both.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Component&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;AI host&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Runs the chat experience, manages conversation context, and decides which tools the model can use. It applies app policy and asks the user for approval when required.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;MCP client&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Lives inside the host and exchanges MCP messages with one server. It requests tool definitions, sends calls, and receives replies.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;MCP server&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Exposes tools such as &lt;code&gt;get_order_status&lt;/code&gt;. It validates inputs, enforces access controls, calls the downstream service, and packages the result.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Orders backend&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Owns order data and the rules for accessing it. In this example, it exposes an HTTP endpoint for checking an order’s status.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fx94tyrqj70r94c22uh13.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fx94tyrqj70r94c22uh13.png" alt=" " width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Lets follow order #1042 through the system
&lt;/h2&gt;

&lt;p&gt;1.&lt;strong&gt;The client discovers which tools are available&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The host needs tool definitions before the model can choose a tool. It can get them ahead of the user’s question or reuse a cached list. When discovery is needed, the MCP client sends tools/list. The client sends a credential with the request. The server validates it and can limit the returned tools to those available to that caller. The list comes back to the client, and the host makes the relevant definitions available to the model.&lt;br&gt;
The list response already contains the description and input schema.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"get_order_status"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Get the current shipping status of an order by ID."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"inputSchema"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"object"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"properties"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;"order_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"integer"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"required"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"order_id"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2.&lt;strong&gt;The model proposes a call and the host checks it&lt;/strong&gt;&lt;br&gt;
From “Where’s my order #1042?”, the model proposes get_order_status with order_id: 1042. The host checks whether the call is allowed and prompts for approval if its policy requires it. Once allowed, the MCP client sends tools/call. Here’s an illustrative request with required _meta fields omitted for readability.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"jsonrpc"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"2.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"method"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"tools/call"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"params"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"get_order_status"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"arguments"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"order_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1042&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The JSON-RPC id pairs the reply with this request. The order_id tells the store which record to look up.&lt;/p&gt;

&lt;p&gt;3.&lt;strong&gt;The MCP server checks access and calls the backend&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The server validates the incoming credential and checks whether the caller has permission to invoke the tool. It also validates the arguments against the tool’s input schema. The host decides whether to send the call. The server decides whether to accept it.&lt;/p&gt;

&lt;p&gt;The handler calls GET /orders/1042/status using a credential intended for the orders API. The backend checks that the authenticated customer is allowed to read order 1042 before returning its status. &lt;/p&gt;

&lt;p&gt;4.&lt;strong&gt;The server formats the result and the host gives it to the model&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The tool handler reads the backend response, maps it to the fields the tool exposes, and can leave internal fields out. The MCP server sends a JSON-RPC reply to the MCP client, using the same request ID. Here’s the data in the reply’s structuredContent field.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"order_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1042&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"shipped"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The client hands the result to the host. The host supplies it to the model, which can now reply:&lt;br&gt;
_Order #1042 has shipped.&lt;br&gt;
_&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why not give the model the REST APIs directly?&lt;/strong&gt;&lt;br&gt;
You can integrate a REST endpoint directly. Define a tool in your model provider’s format, map its arguments to the HTTP request, and have your application execute it with the right credentials. This works well when an app calls a small set of known endpoints. OpenAPI can describe those endpoints, and your host can use that description to build tool definitions.&lt;/p&gt;

&lt;p&gt;As you add hosts or model providers, the work is in each integration. Someone has to make operations discoverable, map arguments and results, and handle errors. With MCP, compatible hosts can use the same server-side tool definition and call contract. Each host still translates that definition into whatever its model expects. The store’s REST endpoint and the code that maps get_order_status to it can be shared across those hosts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What if the customer asks, “Has it arrived yet?&lt;/strong&gt;&lt;br&gt;
The host can give the model the earlier conversation. That lets the model resolve it to order 1042 and propose another lookup with the same ID. The client sends a fresh request, and the backend returns whatever status it currently has.&lt;/p&gt;

&lt;p&gt;If the backend times out, the MCP server can return a tool error with isError: true. The client can pass that result through the host to the model, which can say it couldn’t check the latest status. The earlier “shipped” response is still part of the conversation, but it came from an earlier lookup. MCP tools specification&lt;/p&gt;

&lt;p&gt;The conversation has state because the host retains context. The orders service has state because it stores the order. &lt;/p&gt;

</description>
      <category>mcp</category>
      <category>ai</category>
      <category>api</category>
      <category>architecture</category>
    </item>
    <item>
      <title>A Full Table Scan Isn’t Always a Mistake</title>
      <dc:creator>Tharun Parepalli</dc:creator>
      <pubDate>Fri, 13 Nov 2026 19:01:25 +0000</pubDate>
      <link>https://dev.to/tharun_parepalli/a-full-table-scan-isnt-always-a-mistake-3aj4</link>
      <guid>https://dev.to/tharun_parepalli/a-full-table-scan-isnt-always-a-mistake-3aj4</guid>
      <description>&lt;p&gt;The orders dashboard is slow, so you pull up the Oracle execution plan. There it is: TABLE ACCESS FULL on a million-row table. The query filters on status, and there’s already an index on that column. You run the same query with a different status and see an index range scan. It’s tempting to conclude that Oracle missed an obvious optimization on the first run.&lt;/p&gt;

&lt;p&gt;Now look at what the two queries ask for. One totals nearly every order the system has ever shipped. The other totals a small set of orders held for review. Both have the same SQL shape and use the same filter column, but they ask Oracle to retrieve very different amounts of data. We’ll follow both access paths, see where the work goes, and use the executed plan to check whether the full scan makes sense.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3zfzvb9xf7a6wmn1ksfj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3zfzvb9xf7a6wmn1ksfj.png" alt=" " width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  An index finds the rows. Oracle may still need the table.
&lt;/h2&gt;

&lt;p&gt;An access path is how the database retrieves rows. With a B-tree index on status, Oracle can navigate to the first matching entry and walk the matching leaf entries. That operation appears as INDEX RANGE SCAN. Each entry carries a rowid, which identifies where a row lives in the table.&lt;/p&gt;

&lt;p&gt;If the query also needs amount and the index contains only status, Oracle uses those rowids to fetch amount from the table. In the plan, look for TABLE ACCESS BY INDEX ROWID above the range scan. Oracle can also batch rowids and try to visit rows in block order. That variant appears as TABLE ACCESS BY INDEX ROWID BATCHED. A block is the unit in which Oracle stores and accesses table data. One block can hold many rows.&lt;/p&gt;

&lt;h2&gt;
  
  
  The trade-off depends on how many blocks you touch
&lt;/h2&gt;

&lt;p&gt;With a full scan, Oracle reads the table’s data blocks and applies the filter as it scans. It can read multiple adjacent blocks in a single I/O request. With a range scan that fetches table rows, Oracle reads the matching index entries and then visits the blocks those entries point to.&lt;/p&gt;

&lt;p&gt;Imagine a table with 10,000 blocks. If a query’s matching rows occupy 20 of them, the index can help Oracle skip most of the table. If the matching rows are spread across almost all 10,000 blocks, the index path can visit almost the whole table anyway, on top of reading the index. Batching can improve the order of those visits. It still has to retrieve the rows.&lt;/p&gt;

&lt;p&gt;Oracle’s clustering factor describes how closely table row placement follows a particular index’s key order. A value near the table’s block count suggests that rows encountered close together in index order tend to share blocks. A value near the row count suggests more scattered visits. That’s one reason the same percentage of matching rows can favor different plans on different tables.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fcmpfxcy16mbxyy9vdo5d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fcmpfxcy16mbxyy9vdo5d.png" alt=" " width="800" height="567"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Skew can give the same query two sensible plans
&lt;/h2&gt;

&lt;p&gt;Selectivity is the fraction of rows that satisfy a predicate. When values occur at very different frequencies, the data is skewed. For an orders table where SHIPPED is common and HELD is rare, an average across all statuses can hide the difference between these two queries. A histogram, a statistic that describes the distribution of values in a column, can help Oracle estimate their cardinalities, meaning the numbers of rows expected from each operation.&lt;/p&gt;

&lt;p&gt;The columns you request matter too. An index on (status, amount) could supply both columns needed by the example below and avoid table rowid fetches. That changes the cost comparison. The worked example uses an index on status alone so you can see the cost of going back to the table.&lt;/p&gt;

&lt;h2&gt;
  
  
  Worked example in Oracle
&lt;/h2&gt;

&lt;p&gt;Run this in a scratch schema. The table has one million rows. Every thousandth row is HELD, so those rows are spread across the load. SHIPPED accounts for 900,000 rows. The padding keeps the rows closer in size to a wider application table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plsql"&gt;&lt;code&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;scan_demo_orders&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="no"&gt;LEVEL&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="k"&gt;CASE&lt;/span&gt;
         &lt;span class="k"&gt;WHEN&lt;/span&gt; &lt;span class="no"&gt;MOD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;LEVEL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;THEN&lt;/span&gt; &lt;span class="s1"&gt;'HELD'&lt;/span&gt;
         &lt;span class="k"&gt;WHEN&lt;/span&gt; &lt;span class="no"&gt;MOD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;LEVEL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;900&lt;/span&gt; &lt;span class="k"&gt;THEN&lt;/span&gt; &lt;span class="s1"&gt;'SHIPPED'&lt;/span&gt;
         &lt;span class="k"&gt;ELSE&lt;/span&gt; &lt;span class="s1"&gt;'PENDING'&lt;/span&gt;
       &lt;span class="k"&gt;END&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="no"&gt;MOD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;LEVEL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="no"&gt;RPAD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'x'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'x'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;padding&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;dual&lt;/span&gt;
&lt;span class="no"&gt;CONNECT&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="no"&gt;LEVEL&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;1000000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;scan_demo_orders_status_ix&lt;/span&gt;
    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;scan_demo_orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;BEGIN&lt;/span&gt;
  &lt;span class="no"&gt;DBMS_STATS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;GATHER_TABLE_STATS&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;ownname&lt;/span&gt;          &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;USER&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;tabname&lt;/span&gt;          &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'SCAN_DEMO_ORDERS'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;estimate_percent&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;method_opt&lt;/span&gt;       &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'FOR ALL COLUMNS SIZE 1 FOR COLUMNS SIZE 3 STATUS'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;cascade&lt;/span&gt;          &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;TRUE&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;END&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run each aggregate and display its cursor plan immediately afterward in the same session. The GATHER_PLAN_STATISTICS hint collects execution statistics for the statement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="cm"&gt;/*+ GATHER_PLAN_STATISTICS */&lt;/span&gt; &lt;span class="nb"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;scan_demo_orders&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'HELD'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;DBMS_XPLAN&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;DISPLAY_CURSOR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'ALLSTATS LAST'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="cm"&gt;/*+ GATHER_PLAN_STATISTICS */&lt;/span&gt; &lt;span class="nb"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;scan_demo_orders&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'SHIPPED'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;DBMS_XPLAN&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;DISPLAY_CURSOR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'ALLSTATS LAST'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Illustrative cursor-plan excerpts. These show possible plans and example row counts. Actual plans depend on the optimizer environment.&lt;br&gt;
Predicate: STATUS = 'HELD'&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Predicate: STATUS = 'HELD'
| Id | Operation                             | Name                       | E-Rows | A-Rows |
|----|---------------------------------------|----------------------------|--------|--------|
|  1 |  SORT AGGREGATE                       |                            |      1 |      1 |
|  2 |   TABLE ACCESS BY INDEX ROWID BATCHED | SCAN_DEMO_ORDERS           |   1000 |   1000 |
|  3 |    INDEX RANGE SCAN                   | SCAN_DEMO_ORDERS_STATUS_IX |   1000 |   1000 |

Predicate: STATUS = 'SHIPPED'
| Id | Operation                             | Name                       | E-Rows | A-Rows |
|----|---------------------------------------|----------------------------|--------|--------|
|  1 |  SORT AGGREGATE                       |                            |      1 |      1 |
|  2 |   TABLE ACCESS FULL                   | SCAN_DEMO_ORDERS           | 900000 | 900000 |
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both aggregates return one row. The difference is in the work beneath them. The HELD query can use the index to identify 1,000 rowids and fetch their amounts. The SHIPPED scan emits 900,000 rows after applying the filter. To check how much work that took, look at Buffers in your own cursor plan. It counts logical block accesses. E-Rows and A-Rows show estimated and actual output rows, respectively. For these single-start access operations, you can compare them directly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.oracle.com/en/database/oracle/oracle-database/19/tgsql/optimizer-access-paths.html" rel="noopener noreferrer"&gt;Oracle Database 19c SQL Tuning Guide · Optimizer Access Paths&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.oracle.com/en/database/oracle/oracle-database/19/tgsql/optimizer-statistics-concepts.html" rel="noopener noreferrer"&gt;Oracle Database 19c SQL Tuning Guide · Optimizer Statistics Concepts&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.oracle.com/en/database/oracle/oracle-database/19/arpls/DBMS_XPLAN.html" rel="noopener noreferrer"&gt;Oracle Database 19c PL/SQL Packages and Types Reference · DBMS_XPLAN&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>database</category>
      <category>sql</category>
      <category>performance</category>
      <category>oracle</category>
    </item>
    <item>
      <title>What Really Happens After You Execute a SQL Query in Oracle?</title>
      <dc:creator>Tharun Parepalli</dc:creator>
      <pubDate>Sat, 07 Nov 2026 06:52:47 +0000</pubDate>
      <link>https://dev.to/tharun_parepalli/what-really-happens-after-you-execute-a-sql-query-in-oracle-5948</link>
      <guid>https://dev.to/tharun_parepalli/what-really-happens-after-you-execute-a-sql-query-in-oracle-5948</guid>
      <description>&lt;p&gt;You type this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;total_amount&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;   &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt;   &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt;  &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;country&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'IN'&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt;  &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A few milliseconds later, rows come back. It feels like one step, but it's a pipeline, and most of the interesting decisions happen &lt;strong&gt;before a single row is read&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I work on optimizer internals, so this is my favorite part of the database. Here's the big picture.&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Parse  -&amp;gt;  Optimize  -&amp;gt;  Choose a plan  -&amp;gt;  Execute
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhp3dpmrvbjgokb5ugmad.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhp3dpmrvbjgokb5ugmad.png" alt="Query Life Cycle" width="800" height="437"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Parse:&lt;/strong&gt; validate the SQL and look for an existing cursor to reuse.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Optimize:&lt;/strong&gt; the cost-based optimizer (CBO) explores many ways to answer the query.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Choose a plan:&lt;/strong&gt; the lowest-cost candidate becomes the execution plan.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Execute:&lt;/strong&gt; the plan runs and rows are fetched back to you.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  1. Parse: "Is this even a valid question?"
&lt;/h2&gt;

&lt;p&gt;Oracle first checks that your SQL makes sense:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Syntax check:&lt;/strong&gt; a typo like &lt;code&gt;SELEC&lt;/code&gt; dies here.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Semantic check:&lt;/strong&gt; do the tables and columns exist, and do you have privileges on them?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Shared pool check:&lt;/strong&gt; Oracle hashes the statement text into a &lt;code&gt;SQL_ID&lt;/code&gt; and looks in the &lt;strong&gt;library cache&lt;/strong&gt;.

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Soft parse:&lt;/strong&gt; a matching cursor exists and is reused. Cheap.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hard parse:&lt;/strong&gt; no match, so Oracle runs full optimization. Expensive.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can watch this for your own session:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;   &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;mystat&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt;   &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;statname&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;statistic&lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;statistic&lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt;  &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'parse count (total)'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'parse count (hard)'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run your query, then run this again. If "parse count (hard)" went up, Oracle had to build a new plan from scratch.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Optimize: "What are all the ways I could answer this?"
&lt;/h2&gt;

&lt;p&gt;SQL is declarative: you say &lt;em&gt;what&lt;/em&gt; you want, and the CBO decides &lt;em&gt;how&lt;/em&gt;. The same query can be executed many ways, and the gap between the best and worst plan can be milliseconds versus hours.&lt;/p&gt;

&lt;p&gt;The optimizer considers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Query transformations:&lt;/strong&gt; view merging, subquery unnesting, predicate pushing, join elimination.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Access paths:&lt;/strong&gt; full table scan, index range scan, index fast full scan.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Join order:&lt;/strong&gt; &lt;code&gt;customers -&amp;gt; orders&lt;/code&gt; or &lt;code&gt;orders -&amp;gt; customers&lt;/code&gt;. With many tables, the number of orderings explodes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Join methods:&lt;/strong&gt; nested loops, hash join, sort-merge.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To compare candidates, it estimates &lt;strong&gt;cardinality&lt;/strong&gt; (how many rows each step produces) from &lt;strong&gt;optimizer statistics&lt;/strong&gt;, then computes a &lt;strong&gt;cost&lt;/strong&gt; for each plan.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The optimizer never &lt;em&gt;runs&lt;/em&gt; the candidates. It &lt;em&gt;predicts&lt;/em&gt; which one will be cheapest.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's why stale statistics cause so many "it was fast yesterday" incidents. Bad estimates lead to bad plans. Refresh stats with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;BEGIN&lt;/span&gt;
  &lt;span class="n"&gt;DBMS_STATS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GATHER_TABLE_STATS&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;ownname&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;USER&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;tabname&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'CUSTOMERS'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;cascade&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;TRUE&lt;/span&gt;   &lt;span class="c1"&gt;-- include indexes&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;END&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Choose a plan: "Pick the winner"
&lt;/h2&gt;

&lt;p&gt;The lowest-cost candidate becomes the &lt;strong&gt;execution plan&lt;/strong&gt;, a tree of row sources. To see the predicted plan:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="n"&gt;PLAN&lt;/span&gt; &lt;span class="k"&gt;FOR&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;total_amount&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;   &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt;   &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt;  &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;country&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'IN'&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt;  &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;DBMS_XPLAN&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DISPLAY&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Illustrative output (trimmed):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;| Id | Operation                             | Name                  | Rows |
|----|---------------------------------------|-----------------------|------|
|  0 | SELECT STATEMENT                      |                       |  500 |
|  1 |  HASH GROUP BY                        |                       |  500 |
|* 2 |   HASH JOIN                           |                       |  25K |
|  3 |    TABLE ACCESS BY INDEX ROWID BATCHED| CUSTOMERS             |  500 |
|* 4 |     INDEX RANGE SCAN                  | IDX_CUSTOMERS_COUNTRY |  500 |
|  5 |    TABLE ACCESS FULL                  | ORDERS                | 500K |

Predicate Information (identified by operation id):
   2 - access("O"."CUSTOMER_ID"="C"."ID")
   4 - access("C"."COUNTRY"='IN')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Read it by indentation, innermost first. Oracle uses the index to find Indian customers, builds a hash table from that small set, scans &lt;code&gt;orders&lt;/code&gt; once to probe it, then aggregates.&lt;/p&gt;

&lt;p&gt;If the statistics said 90% of customers were in India, the CBO would likely skip the index entirely and choose a different plan.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Execute: "Now actually do it"
&lt;/h2&gt;

&lt;p&gt;The cursor lifecycle is &lt;strong&gt;Parse -&amp;gt; Bind -&amp;gt; Execute -&amp;gt; Fetch&lt;/strong&gt;. While executing, Oracle:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reads blocks from the &lt;strong&gt;buffer cache&lt;/strong&gt;, or from disk on a miss.&lt;/li&gt;
&lt;li&gt;Uses &lt;strong&gt;undo&lt;/strong&gt; to give your query a read-consistent view of the data.&lt;/li&gt;
&lt;li&gt;Spills to &lt;strong&gt;temp&lt;/strong&gt; if a hash join or sort doesn't fit in PGA memory.&lt;/li&gt;
&lt;li&gt;Streams rows back to the client in fetch batches.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;EXPLAIN PLAN&lt;/code&gt; only shows the &lt;em&gt;prediction&lt;/em&gt;. To see what actually happened:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="cm"&gt;/*+ GATHER_PLAN_STATISTICS */&lt;/span&gt;
       &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;total_amount&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;   &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt;   &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt;  &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;country&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'IN'&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt;  &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;   &lt;span class="k"&gt;TABLE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;DBMS_XPLAN&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DISPLAY_CURSOR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;format&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'ALLSTATS LAST'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Compare &lt;strong&gt;E-Rows&lt;/strong&gt; (estimated) with &lt;strong&gt;A-Rows&lt;/strong&gt; (actual). A big gap between them usually points straight at your problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why this matters as a developer
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Same SQL, different speed&lt;/strong&gt; usually means the optimizer chose a different plan because data or statistics changed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use bind variables&lt;/strong&gt; so Oracle can soft parse instead of hard parsing on every call:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;total_amount&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;   &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt;   &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt;  &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;country&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;country&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt;  &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Indexes are options, not commands.&lt;/strong&gt; Creating one doesn't force the CBO to use it. It only does so if the estimated cost is lower.&lt;/p&gt;

</description>
      <category>oracle</category>
      <category>sql</category>
      <category>database</category>
      <category>performance</category>
    </item>
  </channel>
</rss>
