Live data from Hacker News

How I slashed a SQL query's runtime with two Unix commands

spinellis.gr

31–40 of 91 posts

Re: How I slashed a SQL query's runtime with two Unix commands

#31
I don't know mysql/mariaDB that well, but I know in ms sql server that doing a DISTINCT on a Function([Column]) would not use an index on [Column].

In this case it would be much faster to have an intermediate table or column with that statistic that could then be properly indexed.

Re: How I slashed a SQL query's runtime with two Unix commands

#32
post #8

Earlier quoted context omitted.

The query includes `date_format(created_at, '%x%v1') as week_commit` - meaning it would need to be computed for every row.

Only for every returned row. The projection does not affect the underlying result set, it merely "shapes" the results you have received. WHERE clauses are always run before SELECT

The query in the article has no WHERE clause. It must calculate that function for every row in `commits`.

Re: How I slashed a SQL query's runtime with two Unix commands

#33

I don't know mysql/mariaDB that well, but I know in ms sql server that doing a DISTINCT on a Function([Column]) would not use an index on [Column]. In this case it would be much faster to have an intermediate table or column with that statistic that could then be properly indexed.

It even has a name - SARGable - able to use an index on the [S]earch [ARG]uments but still easy to get tripped up.

https://www.definitions.net/definition/sargable

https://stackoverflow.com/questions/799584/what-makes-a-sql-...

Re: How I slashed a SQL query's runtime with two Unix commands

#34
post #27

The distinct on the date format is the problem, probably should use a separate query on each table to fill in the date output as a new column first, and then join afterwards. Also wrong tool for the job comes to mind. MariaDB/MySQL are just not great at joins and query planning, and at this scale, especially with all the work involved to export and use unix tools, why not use any of the columnar data warehouses that…

I highly recommend Clickhouse for this. It is blazing fast and can do over 100GB/s on a single modern machine if you have enough RAM. And it is very easy to install and configure.

+1 for ClickHouse. But it's the kind of software you need to figure out before you scale beyond a single node.

Fast as hell and surprisingly performance in low memory conditions.

Re: How I slashed a SQL query's runtime with two Unix commands

#37

Am I missing something or is this blog post just someone discovering that ETL exists

Lots of "modern" blog posts seem like people rediscovering that X exists.

A consequence of many skipping on traditional learning processes and how developers are now also forced into castings and portfolios.

Re: How I slashed a SQL query's runtime with two Unix commands

#40

I think the date_format call forces a table scan, the query could be rewritten to be much faster. Optimizing the sql statement seems less error prone than extracting the data to text files.

There's no predicate or limit or inner join that could reduce the set of rows, so the table might as well be scanned.
Post reply on HN