<?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: Protap Singh Jacky</title>
    <description>The latest articles on DEV Community by Protap Singh Jacky (@protapsingh).</description>
    <link>https://dev.to/protapsingh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1022160%2F3e40ff80-bc59-4592-8be2-686644042c7b.jpeg</url>
      <title>DEV Community: Protap Singh Jacky</title>
      <link>https://dev.to/protapsingh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/protapsingh"/>
    <language>en</language>
    <item>
      <title>All you need to know about table partition in SQL</title>
      <dc:creator>Protap Singh Jacky</dc:creator>
      <pubDate>Tue, 07 Feb 2023 05:47:20 +0000</pubDate>
      <link>https://dev.to/protapsingh/all-you-need-to-know-about-table-partition-in-sql-5e0a</link>
      <guid>https://dev.to/protapsingh/all-you-need-to-know-about-table-partition-in-sql-5e0a</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is table partition ? How to create a table partition?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Table partitioning is a database management technique that allows breaking down a large table into smaller, more manageable parts, called partitions. This can improve query performance and reduce maintenance overhead by allowing certain operations to be performed on only a portion of the data.&lt;/p&gt;

&lt;p&gt;Here is an example of table partitioning in SQL using the PostgreSQL database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE sales (
    sales_id serial primary key,
    sales_date date not null,
    product_id int not null,
    quantity int not null,
    price decimal not null
) PARTITION BY RANGE (sales_date);

CREATE TABLE sales_q1_2022 PARTITION OF sales
FOR VALUES FROM ('2022-01-01') TO ('2022-03-31');

CREATE TABLE sales_q2_2022 PARTITION OF sales
FOR VALUES FROM ('2022-04-01') TO ('2022-06-30');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the sales table is partitioned by sales_date column and two partitions sales_q1_2022 and sales_q2_2022 are created for the first and second quarter of 2022 respectively.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to select data from partitioned table?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To use a SELECT query on a partitioned table, you can simply query the parent table and the query will be automatically redirected to the relevant partitions based on the data being requested. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM sales WHERE sales_date BETWEEN '2022-01-01' AND '2022-03-31';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, the SELECT query will return the data from the sales_q1_2022 partition, since the data being requested is within the range specified in that partition.&lt;/p&gt;

&lt;p&gt;It's also possible to explicitly query a specific partition, if needed. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM sales_q1_2022;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To retrieve data from a table that is partitioned into multiple partitions, you can use a SELECT query that combines data from multiple partitions. Here's an example in SQL:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM sales WHERE sales_date BETWEEN '2022-01-01' AND '2022-06-30';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, the SELECT query will return the data from both the sales_q1_2022 and sales_q2_2022 partitions, since the data being requested is within the range specified in both partitions.&lt;/p&gt;

&lt;p&gt;You can also use the UNION operator to combine the results from multiple SELECT statements, one for each partition. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM sales_q1_2022
UNION
SELECT * FROM sales_q2_2022;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will return the data from both the sales_q1_2022 and sales_q2_2022 partitions in a single result set.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to delete table partition?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To delete a table partition in SQL, you can use the ALTER TABLE statement with the DROP PARTITION option. The syntax is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER TABLE &amp;lt;table_name&amp;gt; DROP PARTITION &amp;lt;partition_name&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace table_name with the name of the table, and partition_name with the name of the partition you want to delete.&lt;/p&gt;

&lt;p&gt;It's important to note that the exact syntax may vary depending on the specific database management system (DBMS) you're using.&lt;/p&gt;

</description>
      <category>tooling</category>
      <category>productivity</category>
      <category>discuss</category>
    </item>
  </channel>
</rss>
