presto: Cassandra connector IN query very slow planning on large list

A query like -

select col1
from table
where col2 in (<long list of integers>)
and col3 in (<long list of string>)
and col4 in (<another long list of integers>)
and col1 is not null
group by col1;

takes more than 5 minutes just planning. My cassandra table being queried has a lot of partitions and list length for IN query I was experimenting with was anywhere between 50 to 200. <col2, col3, col4> together form the partition keys so I don’t imagine a full table scan to take place during planning or execution. Any ideas?

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 34 (32 by maintainers)

Most upvoted comments

oh, btw, @aandis thank you for your in-depth analysis!

Let me know if concurrent queries sound like a good idea and I’ll start working on it.

I am do not think running concurrent queries is a good idea, let’s keep this option as a last resort.

If you have a tuple domain with multiple columns with values set, then the query which is send to Cassandra should use IN predicate as your original Presto query.

SELECT DISTINCT col2,col3,col4 FROM keyspace.table WHERE col2 IN (17491) AND col3 IN (12, 13, 14) AND col4 IN ('val1', 'val2');

Alright, it took some doing but I ended up setting presto up from source and adding debug statements for profiling this.

Problem

Turns out, for IN query over multiple columns, cassandra connector executes select distinct for each combination of values for the columns, which takes a long time to finish because the number of such queries is huge. Following log statements should make it clear

2018-05-28T13:32:26.929Z	DEBUG	query-execution-5	com.facebook.presto.execution.QueryStateMachine	Query 20180528_133226_00003_6yerz is PLANNING
2018-05-28T13:32:27.316Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.CassandraMetadata	Inside getTableLayouts
2018-05-28T13:32:27.316Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.CassandraPartitionManager	Inside getPartitions
2018-05-28T13:32:27.316Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.CassandraPartitionManager	Fetching cassandra partitions.
2018-05-28T13:32:27.317Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.CassandraPartitionManager	Inside getCassandraPartitions
2018-05-28T13:32:27.317Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.CassandraPartitionManager	Getting partition keyset
2018-05-28T13:32:27.325Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.CassandraPartitionManager	Recieved partition keyset with size 44408
2018-05-28T13:32:27.325Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.CassandraPartitionManager	Partition keyset not empty. Looping over set

2018-05-28T13:32:27.325Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.NativeCassandraSession	Inside Native#getPartitions
2018-05-28T13:32:27.325Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.NativeCassandraSession	Native#queryPartitionKeys started
2018-05-28T13:32:27.336Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.NativeCassandraSession	Executing SELECT DISTINCT col2,col3,col4 FROM keyspace.table WHERE col2=17491 AND col3=12 AND col4='val1';
2018-05-28T13:32:27.356Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.NativeCassandraSession	queryPartitionKeys finished!
2018-05-28T13:32:27.356Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.NativeCassandraSession	Native#getPartitions finished

2018-05-28T13:32:27.356Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.NativeCassandraSession	Inside Native#getPartitions
2018-05-28T13:32:27.356Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.NativeCassandraSession	Native#queryPartitionKeys started
2018-05-28T13:32:27.356Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.NativeCassandraSession	Executing SELECT DISTINCT col2,col3,col4 FROM keyspace.table WHERE col2=17491 AND col3=12 AND col4='val2';
2018-05-28T13:32:27.359Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.NativeCassandraSession	queryPartitionKeys finished!
2018-05-28T13:32:27.359Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.NativeCassandraSession	Native#getPartitions finished

2018-05-28T13:32:27.360Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.NativeCassandraSession	Inside Native#getPartitions
2018-05-28T13:32:27.360Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.NativeCassandraSession	Native#queryPartitionKeys started
2018-05-28T13:32:27.360Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.NativeCassandraSession	Executing SELECT DISTINCT col2,col3,col4 FROM keyspace.table WHERE col2=17491 AND col3=13 AND col4='val1';
2018-05-28T13:32:27.363Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.NativeCassandraSession	queryPartitionKeys finished!
2018-05-28T13:32:27.364Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.NativeCassandraSession	Native#getPartitions finished

2018-05-28T13:32:27.364Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.NativeCassandraSession	Inside Native#getPartitions
2018-05-28T13:32:27.364Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.NativeCassandraSession	Native#queryPartitionKeys started
2018-05-28T13:32:27.365Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.NativeCassandraSession	Executing SELECT DISTINCT col2,col3,col4 FROM keyspace.table WHERE col2=17491 AND col3=13 AND col4='val2';
2018-05-28T13:32:27.367Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.NativeCassandraSession	queryPartitionKeys finished!
2018-05-28T13:32:27.367Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.NativeCassandraSession	Native#getPartitions finished

2018-05-28T13:32:27.367Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.NativeCassandraSession	Inside Native#getPartitions
2018-05-28T13:32:27.368Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.NativeCassandraSession	Native#queryPartitionKeys started
2018-05-28T13:32:27.368Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.NativeCassandraSession	Executing SELECT DISTINCT col2,col3,col4 FROM keyspace.table WHERE col2=17491 AND col3=14 AND col4='val1';
2018-05-28T13:32:27.372Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.NativeCassandraSession	queryPartitionKeys finished!
2018-05-28T13:32:27.372Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.NativeCassandraSession	Native#getPartitions finished

2018-05-28T13:32:27.372Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.NativeCassandraSession	Inside Native#getPartitions
2018-05-28T13:32:27.373Z	DEBUG	Query-20180528_133226_00003_6yerz-146	com.facebook.presto.cassandra.NativeCassandraSession	Native#queryPartitionKeys started
..
..
<Executes for all possible combination of <col2, col3, col4>

This goes on for close to a minute.

Possible fixes

  1. Skip select distinct queries - I am not sure I understand the reason for SELECT DISTINCT col2,col3,col4 FROM keyspace.table WHERE col2=? AND col3=? AND col4=? queries when the columns being queried form the complete partition key set. <col2, col3, col4> in this case form the complete partition keyset, so SELECT DISTINCT will always return atmost 1 record.
  2. Execute them concurrently - In case, we have to fire the above queries, we could fire them concurrently using executeAsync. Something similar to execute_concurrent_with_args

I am open to sending a patch to fix this based on what’s decided here.