📔
Databases
  • Table of contents
  • SQL
    • Getting Started
      • PgAdmin Tool
    • Data Types
      • Arrays
      • Date/Time/Stamps
      • JSON
      • Internal Functions
      • Sequences
      • User Defined Data Types
    • Database
      • Schema
    • Table
      • Aggregation
      • Usefull Functions
      • Combining Tables
      • Constraints
      • Common Table Expression
      • GROUP BY and HAVING
      • OPERATORS
      • ORDER BY and DISTINCT
      • Views
      • WHERE Clause
    • Order of SQL Execution
    • Advance Table
      • Internals
      • Managing Tables
      • Partitioning Tables
      • Pivotal or Crosstab Tables
    • Joins
      • Cross & Natural Joins
      • Full, Multiple & Self Joins
      • Inner Join
      • Left and Right JOIN
    • Functions
      • Cursors
      • PL/pgSQL
      • Stored Procedures
      • Triggers
        • More on Triggers
    • Indexing
      • Custom Indexes
      • GIN Index
      • Indexes
      • SQL
      • Unique Index
    • Summarization
      • SubQueries
      • Window
  • MongoDB
    • Mongo Administration
    • MongoDB Aggregation
    • MQL
  • Redis
  • Cassandra
    • CQL
    • Data Modelling
      • Advance Data Modelling
    • Cassandra Administration
    • Cassandra Features
Powered by GitBook
On this page

Was this helpful?

  1. SQL
  2. Indexing

Custom Indexes

Example :

CREATE a index for social security number , which is the the format of 1111-222-nnnn, you have to index the last 4 characters

CREATE TABLE if not exists ssn
(
    ssn text
);

INSERT INTO ssn (ssn)
values ('111-11-0100'),
       ('222-22-0120'),
       ('333-33-0140'),
       ('444-44-0160');

select *
from ssn;

explain
select *
from ssn;

CREATE OR REPLACE FUNCTION FN_FIX_SSN(TEXT) RETURNS text AS
$$
BEGIN
    return substring($1, 8) || replace(substring($1, 1, 7), '-', '');

end ;
$$ LANGUAGE plpgsql IMMUTABLE;

SELECT ssn, FN_FIX_SSN(ssn)
from ssn;

drop function fn_ssn_compare cascade;

CREATE OR REPLACE FUNCTION fn_ssn_compare(text,text) returns int as
$$
    BEGIN
        if FN_FIX_SSN($1) < FN_FIX_SSN($2) then return -1;
        elseif FN_FIX_SSN($1) > FN_FIX_SSN($2) then return 1;
        else
            return 0;
        end if;

    end;
$$ language plpgsql immutable;

CREATE OPERATOR CLASS op_class_ssn_ops1
FOR TYPE text USING btree
as
        OPERATOR 1 <,
        OPERATOR 2 <=,
        OPERATOR 3 =,
        OPERATOR 4 >=,
        operator 5 >,
        function 1 fn_ssn_compare(text,text);

CREATE INDEX idx_ssn on ssn(ssn op_class_ssn_ops1);

explain
select *
from ssn where ssn.ssn = '222-22-0120';

set enable_seqscan = 'off';
show enable_seqscan;
PreviousIndexingNextGIN Index

Last updated 3 years ago

Was this helpful?