This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create extension if not exists "uuid-ossp"; | |
create table business ( | |
id uuid default uuid_generate_v4() primary key, | |
name character varying not null | |
); | |
create table customer ( | |
id uuid default uuid_generate_v4() primary key, | |
business_id uuid references business(id), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create extension if not exists pg_trgm; | |
create extension if not exists unaccent; | |
create text search configuration en ( copy = pg_catalog.english ); | |
alter text search configuration en | |
alter mapping for asciiword, asciihword, hword_asciipart, word, hword, hword_part | |
with unaccent, english_stem; | |
set default_text_search_config = 'en'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create table if not exists customer_search ( | |
customer_id uuid, | |
contact_id uuid, | |
primary key(customer_id, contact_id), | |
-- contact method type, for ex. 'Phone' or 'Email' | |
contact_method text, | |
-- term is a value computed from the contact method | |
term text, | |
-- term_display is used as display value instead of the term column which is more technical | |
term_display character varying, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create function customer_search_customer_update() | |
returns trigger | |
security definer | |
language plpgsql | |
as $$ | |
begin | |
update customer_search | |
set is_stale=true, | |
name=new.name, | |
deleted_at=new.deleted_at |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create function customer_search_email_insert() | |
returns trigger | |
security definer | |
language plpgsql | |
as $$ | |
begin | |
insert into customer_search(customer_id, contact_method, is_stale, term, contact_id) | |
values(new.customer_id, 'Email', true, new.email_address, new.id); | |
return new; | |
end; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create function refresh_customer_search(_customer_id uuid, _contact_id uuid) | |
returns customer_search | |
security definer | |
language sql | |
As $$ | |
with contact_method as ( | |
-- For (_customer_id, _contact_id) | |
select contact.*, | |
customer.name, | |
customer.business_id, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- build a view to show all data that combines | |
create view quick_customer_search as | |
select customer_search.* | |
from customer_search | |
-- * records that are not marked as stale | |
where customer_search.is_stale = false | |
union all | |
select r.* | |
from customer_search | |
-- * records that are stale => recompute the most up to date fields on the fly |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- global fuzzy search | |
-- Search for customers matching the search term in customer details | |
select | |
qcs."name", | |
qcs."photo", | |
qcs."customer_id", | |
-- fetch all contact methods for the customer that match the search query | |
-- * contact method term display (national number for phone, email address for email) | |
array_agg(qcs."term_display"), | |
-- type of contact method (Email of Phone) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Cluster | Phase | Importer | Mode | Write Concern | Timeout | Executor | All Executors | |
---|---|---|---|---|---|---|---|---|
DE only | Staging | mongoimport | upsert | primary only | N/A | 0.98 MB/s | 18.62 MB/s | |
DE only | Staging | tymongoimport | upsert | primary only | N/A | 1.44 MB/s | 27.36 MB/s | |
DE only | Staging | spark | upsert | primary only | N/A | 6.15 MB/s | 117.33 MB/s | |
DE + US | Staging | mongoimport | upsert | all | 0 (indefinite) | 0.38 MB/s | 4.56 MB/s | |
DE + US | Staging | tymongoimport | upsert | all | 0 (indefinite) | 1.22 MB/s | 14.64 MB/s | |
DE + US | Staging | spark | upsert | all | 0 (indefinite) | 0.85 MB/s | 16.15 MB/s | |
DE + US | Production | tymongoimport | upsert | all | 0 (indefinite) | 0.36 MB/s | 4.32 MB/s | |
DE + US | Production | spark | upsert | all | 0 (indefinite) | 0.19 MB/s | 3.61 MB/s |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Cluster | Version | Mode | Write Concern | Timeout | Executor | All Executors | |
---|---|---|---|---|---|---|---|
DE + US | 3.2 | upsert | all | 0 (indefinite) | 1.22 MB/s | 14.64 MB/s | |
DE + US | 3.4 | upsert | all | 0 (indefinite) | 3.12 MB/s | 37.44 MB/s |
OlderNewer