Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
SQL

SQL: Use Partial Indexes to Index Only Relevant Rows

- 28.05.26 - ErcanOPAK

🎯 Index Only What You Query

Full indexes waste space. Partial indexes index only rows matching a WHERE clause. Smaller, faster, more efficient.

📝 Basic Partial Index

-- Index only active users (not soft-deleted)
CREATE INDEX idx_active_users_email ON users(email) 
WHERE deleted_at IS NULL;

-- Query uses this index automatically
SELECT * FROM users 
WHERE email = 'alice@example.com' 
  AND deleted_at IS NULL;

-- Index only orders with high value
CREATE INDEX idx_high_value_orders ON orders(customer_id, order_date)
WHERE total_amount > 1000;

-- Only large orders indexed (saves space)

🎯 Real-World Examples

-- Index only pending orders (most frequently queried)
CREATE INDEX idx_pending_orders ON orders(created_at)
WHERE status = 'pending';

-- Index only premium customers
CREATE INDEX idx_premium_users ON users(last_login)
WHERE subscription_tier = 'premium' AND active = true;

-- Index only current year invoices
CREATE INDEX idx_current_year_invoices ON invoices(invoice_date)
WHERE invoice_date >= '2024-01-01';

-- Exclude test data
CREATE INDEX idx_prod_products ON products(sku, name)
WHERE is_test = false;

✅ Benefits

  • Smaller index size: 70-90% smaller than full index
  • Faster queries: Scan fewer index pages
  • Less maintenance: Updates on excluded rows skip index
  • Less disk I/O: Smaller index fits in memory

💡 When to Use

  • 95% of queries filter on a specific status value
  • Soft-deleted rows rarely queried
  • Historical data vs active data
  • Test data that shouldn’t be indexed
  • Only recent data is queried frequently

“Full index on 50M rows was 8GB. Partial index on active users (WHERE active=true) was 500MB. Query speed improved 10x. Always consider partial indexes.”

— Database Administrator

Related posts:

Replace duplicate spaces with a single space in SQL

Replace duplicate spaces with a single space in SQL

SQL — BETWEEN Can Return Unexpected Rows

SQL — BETWEEN Can Return Unexpected Rows

SQL Server: Fix a CHECK Constraint That Silently Lets NULL Bypass the Rule It Was Meant to Enforce

SQL Server: Fix a CHECK Constraint That Silently Lets NULL Bypass the Rule It Was Meant to Enforce

Views: 18
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

.NET Core: Understanding Middleware Order (It Matters!)
C#: Use Span for High-Performance Memory Access Without Allocation

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,079 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 868 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 631 views
  8. 08Average of all values in a column that are not zero in SQL 584 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 561 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com