Wed Nov 18 2020Journal

Applicative SQL best practices

avatar
Xavier Bruhiere
linkedintwitterfacebook
intro picture
HEY:

Work in progress.

Performance

Monitoring

Checking table sizes details and pretty printing it.

SELECT
   relname AS table_name,
   pg_size_pretty(pg_total_relation_size(relid)) AS total,
   pg_size_pretty(pg_relation_size(relid)) AS internal,
   pg_size_pretty(pg_table_size(relid) - pg_relation_size(relid)) AS external,
   pg_size_pretty(pg_indexes_size(relid)) AS indexes
FROM pg_catalog.pg_statio_user_tables
ORDER BY pg_total_relation_size(relid) DESC;

Best practices

If you are looking for a COUNT, rather than running the query to spot the count of rows at the bottom of the screen, use a subquery that counts the rows for you.

select count(*) from

(
    select
    userid
    from userschema.usertable
    where market = 'UK'
    and payingcustomer = 1
) as derivedtable;




#application#design#sql

Read Next

Use domain-driven design to architect your cloud apps

Domain-driven design (DDD) is a set of strategies and tools that can help you design systems and manage complexities. We will focus on how it can be used to architect cloud applications—specifically how to integrate it into existing stacks, and especially how it can help you build powerful event-sourcing systems.