SlideShare uma empresa Scribd logo
1 de 29
Baixar para ler offline
Chicago 2015
CQL: This is not the SQL
you are looking for
Aaron Ploetz
Wait... CQL is not SQL?
l CQL3 introduced in Cassandra 1.1.
l CQL is beneficial to new users who have a relational background
(which is most of us).
l However similar, CQL is NOT a direct implementation of SQL.
l New users leave themselves open to issues and frustration when
they use CQL with SQL-based expectations.
$ whoami
l Aaron Ploetz
l @APloetz
l Lead Database Engineer
l Using Cassandra since version 0.8.
l Contributor to the Cassandra tag on
l 2014/15 DataStax MVP for Apache Cassandra
1 SQL features/keywords not present in CQL
2 Differences between CQL and SQL keywords
3 Secondary Indexes
4 Anti-Patterns
5 Questions
SQL features/keywords not present in CQL
l JOINs
l LIKE
l Subqueries
l Aggregation
l Arithmetic
l Except for counters and collections.
Differences between CQL and SQL keywords
l WHERE
l PRIMARY KEY
l ORDER BY
l IN
l DISTINCT
l COUNT
l LIMIT
l INSERT vs. UPDATE (“upsert”)
WHERE
l Only supports AND, IN, =, >, >=, <, <=.
l Some only function under certain conditions.
l Also: CONTAINS, CONTAINS KEY for indexed collections.
l Does not exist: OR, !=
l Conditions can only operate on PRIMARY KEY components, and
in the defined order of the keys.
WHERE (cont)
l SELECT * FROM shipcrewregistry WHERE
shipname='Serenity';
l Start with partition key(s); cannot skip PRIMARY KEY
components.
l CREATE TABLE shipcrewregistry
(shipname text, lastname text, firstname
text, citizenid uuid,
aliases set<text>, PRIMARY KEY
(shipname, lastname, firstname,
citizenid));
ALLOW FILTERING
l Actually I lied, you can skip primary key components if you apply
the ALLOW FILTERING clause.
l SELECT * FROM shipcrewregistry WHERE
lastname='Washburne';
ALLOW FILTERING (cont)
l SELECT * FROM shipcrewregistry WHERE
lastname='Washburne' ALLOW FILTERING;
l But I don't recommend that.
l ALLOW FILTERING pulls back all rows and then applies your
WHERE conditions.
l The folks at DataStax have proposed some alternate names...
l Bottom line, if you are using ALLOW FILTERING, you are doing it
wrong.
PRIMARY KEY
l PRIMARY KEYs function differently between Cassandra and
relational databases.
l Cassandra uses primary keys to determine data distribution and
on-disk sort order.
l Partition keys are the equivalent of “old school” row keys.
l Clustering keys determine on-disk sort order within a
partitioning key.
ORDER BY
l One of the most misunderstood aspects of CQL.
l Can only order by clustering columns, in the key order of the
clustering columns listed in the table definition (CLUSTERING
ORDER).
l Which means, that you really don't need ORDER BY.
l So what does it do? It can reverse the sort direction (ASCending
vs. DESCending) of the first clustering column.
PRIMARY KEY / ORDER BY Example:
Table Definition
l CREATE TABLE postsByUserYear
l (userid text, year bigint, tag text, posttime
timestamp, content text, postid UUID,
PRIMARY KEY ((userid, year), posttime,
tag)) WITH CLUSTERING ORDER BY
(posttime desc, tag asc);
PRIMARY KEY / ORDER BY Example:
Queries
l SELECT * FROM postsByUserYear WHERE userid='2';
l SELECT * FROM postsByUserYear ORDER BY
posttime;
l SELECT * FROM postsByUserYear WHERE userid='2'
AND year=2015 ORDER BY posttime DESC;
l SELECT * FROM postsByUserYear WHERE userid='2'
AND year=2015 ORDER BY tag;
IN
l Can only operate on the last partition key and/or the last clustering
key.
l And only when the first partition/clustering keys are restricted
by an equals relation.
l Does not perform well...especially with large clusters.
Testing IN
l CREATE TABLE bladerunners (id text, type text, ts
timestamp, name text, data text, PRIMARY KEY (id));
Testing IN (cont)
l  SELECT * FROM bladerunners WHERE id IN
('B26354','B26354');
DISTINCT
l Returns a list of the queried partition keys.
l Can only operate on partition key column(s).
l In Cassandra DISTINCT returns the partition (row) keys, so it is a
fairly light operation (relative to the size of the cluster and/or data
set).
l Whereas in the relational world, DISTINCT is a very resource
intensive operation.
COUNT
l Counts the number of rows returned, dependent on the WHERE
clause.
l Does not aggregate.
l Similar to its RDBMs counterpart.
l Can be (inadvertently) restricted by LIMIT.
l Resource intensive command; especially because it has to scan
each row in the table (which may be on different nodes), and apply
the WHERE conditions.
Limit
l Limits your query to N rows (where N is a positive integer).
l SELECT * FROM bladerunners LIMIT 2;
l Does not allow you to specify a start point.
l You cannot use LIMIT to “page” through your result set.
Cassandra “Upserts”
l Under the hood, INSERT and UPDATE are treated the same by
Cassandra.
l Colloquially known as an “Upsert.”
l Both INSERT and UPDATE operations require the complete
PRIMARY KEY.
So why the different syntax?
l Flexibility. Some situations call for one or the other.
l Counter columns/tables can only be incremented with an
UPDATE.
l INSERTs can save you some dev time in the application layer if
your PRIMARY KEY changes.
“Upsert” example
l UPDATE bladerunners SET data='This guy
is a one-man slaughterhouse.',name='Harry
Bryant',ts='2015-03-30 14:47:00-0600',type='Captain'
WHERE id='B16442';
l UPDATE bladerunners SET data = 'Drink
some for me, huh pal?' WHERE id='B16442';
“Upsert” example (cont)
l INSERT INTO bladerunners (id, type, ts, data, name)
VALUES ('B29591','Blade Runner','2015-03-30
14:34:00-0600','Captain Bryant would like a
word.','Eduardo Gaff');
l INSERT INTO bladerunners (id,data) VALUES
('B29591','It''s too bad she won't live. But then again,
who does?');
Secondary Indexes
l Cassandra provides secondary indexes to allow queries on non-
partition key columns.
l In 2.1.x you can even create indexes on collections and user
defined types.
l Designed for convenience, not for performance.
l Does not perform well on high-cardinality columns.
l Extremely low cardinality is also not a good idea.
l Low performance on a frequently updated column.
l In my opinion, try to avoid using them all together.
Anti-Patterns
l Multi-Key queries: IN
l Secondary Index queries
l DELETEs or INSERTing null values
Summary
l While CQL is designed to make use of our previous experience
using SQL, it is important to remember that the two do not behave
the same.
l Even if you are at an expert level in SQL, read the CQL
documentation before making any assumptions.
Additional Reading
l Getting Started with Time Series Data Modeling –
Patrick McFadin
l SELECT – DataStax CQL 3.1 documentation
l Counting Keys in Cassandra –
Richard Low
l Cassandra High Availability –
Robbie Strickland
Questions?

Mais conteúdo relacionado

Semelhante a CQL is not SQL: Key differences between CQL and SQL

Advanced MySQL Query Optimizations
Advanced MySQL Query OptimizationsAdvanced MySQL Query Optimizations
Advanced MySQL Query OptimizationsDave Stokes
 
Use Your MySQL Knowledge to Become an Instant Cassandra Guru
Use Your MySQL Knowledge to Become an Instant Cassandra GuruUse Your MySQL Knowledge to Become an Instant Cassandra Guru
Use Your MySQL Knowledge to Become an Instant Cassandra GuruTim Callaghan
 
Exploring collections with example
Exploring collections with exampleExploring collections with example
Exploring collections with examplepranav kumar verma
 
Dbms ii mca-ch7-sql-2013
Dbms ii mca-ch7-sql-2013Dbms ii mca-ch7-sql-2013
Dbms ii mca-ch7-sql-2013Prosanta Ghosh
 
Ben Finkel- Using the order by clause.pptx
Ben Finkel- Using the order by clause.pptxBen Finkel- Using the order by clause.pptx
Ben Finkel- Using the order by clause.pptxStephenEfange3
 
Sql ch 12 - creating database
Sql ch 12 - creating databaseSql ch 12 - creating database
Sql ch 12 - creating databaseMukesh Tekwani
 
How to leave the ORM at home and write SQL
How to leave the ORM at home and write SQLHow to leave the ORM at home and write SQL
How to leave the ORM at home and write SQLMariaDB plc
 
Arrays and lists in sql server 2008
Arrays and lists in sql server 2008Arrays and lists in sql server 2008
Arrays and lists in sql server 2008nxthuong
 
Structure query language (sql)
Structure query language (sql)Structure query language (sql)
Structure query language (sql)Nalina Kumari
 
Learn Ruby by Reading the Source
Learn Ruby by Reading the SourceLearn Ruby by Reading the Source
Learn Ruby by Reading the SourceBurke Libbey
 
Structure Query Language (SQL).pptx
Structure Query Language (SQL).pptxStructure Query Language (SQL).pptx
Structure Query Language (SQL).pptxNalinaKumari2
 
Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standardsAlessandro Baratella
 

Semelhante a CQL is not SQL: Key differences between CQL and SQL (20)

Advanced MySQL Query Optimizations
Advanced MySQL Query OptimizationsAdvanced MySQL Query Optimizations
Advanced MySQL Query Optimizations
 
Chapter2
Chapter2Chapter2
Chapter2
 
Use Your MySQL Knowledge to Become an Instant Cassandra Guru
Use Your MySQL Knowledge to Become an Instant Cassandra GuruUse Your MySQL Knowledge to Become an Instant Cassandra Guru
Use Your MySQL Knowledge to Become an Instant Cassandra Guru
 
Exploring collections with example
Exploring collections with exampleExploring collections with example
Exploring collections with example
 
Dbms ii mca-ch7-sql-2013
Dbms ii mca-ch7-sql-2013Dbms ii mca-ch7-sql-2013
Dbms ii mca-ch7-sql-2013
 
Cassandra20141113
Cassandra20141113Cassandra20141113
Cassandra20141113
 
SQL
SQLSQL
SQL
 
Ben Finkel- Using the order by clause.pptx
Ben Finkel- Using the order by clause.pptxBen Finkel- Using the order by clause.pptx
Ben Finkel- Using the order by clause.pptx
 
Sql ch 12 - creating database
Sql ch 12 - creating databaseSql ch 12 - creating database
Sql ch 12 - creating database
 
Sql
SqlSql
Sql
 
How to leave the ORM at home and write SQL
How to leave the ORM at home and write SQLHow to leave the ORM at home and write SQL
How to leave the ORM at home and write SQL
 
Arrays and lists in sql server 2008
Arrays and lists in sql server 2008Arrays and lists in sql server 2008
Arrays and lists in sql server 2008
 
Structure query language (sql)
Structure query language (sql)Structure query language (sql)
Structure query language (sql)
 
Learn Ruby by Reading the Source
Learn Ruby by Reading the SourceLearn Ruby by Reading the Source
Learn Ruby by Reading the Source
 
Ankit
AnkitAnkit
Ankit
 
Structure Query Language (SQL).pptx
Structure Query Language (SQL).pptxStructure Query Language (SQL).pptx
Structure Query Language (SQL).pptx
 
Cassandra20141009
Cassandra20141009Cassandra20141009
Cassandra20141009
 
Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standards
 
Structured Query Language
Structured Query LanguageStructured Query Language
Structured Query Language
 
MYSQL.ppt
MYSQL.pptMYSQL.ppt
MYSQL.ppt
 

Mais de DataStax Academy

Forrester CXNYC 2017 - Delivering great real-time cx is a true craft
Forrester CXNYC 2017 - Delivering great real-time cx is a true craftForrester CXNYC 2017 - Delivering great real-time cx is a true craft
Forrester CXNYC 2017 - Delivering great real-time cx is a true craftDataStax Academy
 
Introduction to DataStax Enterprise Graph Database
Introduction to DataStax Enterprise Graph DatabaseIntroduction to DataStax Enterprise Graph Database
Introduction to DataStax Enterprise Graph DatabaseDataStax Academy
 
Introduction to DataStax Enterprise Advanced Replication with Apache Cassandra
Introduction to DataStax Enterprise Advanced Replication with Apache CassandraIntroduction to DataStax Enterprise Advanced Replication with Apache Cassandra
Introduction to DataStax Enterprise Advanced Replication with Apache CassandraDataStax Academy
 
Cassandra on Docker @ Walmart Labs
Cassandra on Docker @ Walmart LabsCassandra on Docker @ Walmart Labs
Cassandra on Docker @ Walmart LabsDataStax Academy
 
Cassandra 3.0 Data Modeling
Cassandra 3.0 Data ModelingCassandra 3.0 Data Modeling
Cassandra 3.0 Data ModelingDataStax Academy
 
Cassandra Adoption on Cisco UCS & Open stack
Cassandra Adoption on Cisco UCS & Open stackCassandra Adoption on Cisco UCS & Open stack
Cassandra Adoption on Cisco UCS & Open stackDataStax Academy
 
Data Modeling for Apache Cassandra
Data Modeling for Apache CassandraData Modeling for Apache Cassandra
Data Modeling for Apache CassandraDataStax Academy
 
Production Ready Cassandra
Production Ready CassandraProduction Ready Cassandra
Production Ready CassandraDataStax Academy
 
Cassandra @ Netflix: Monitoring C* at Scale, Gossip and Tickler & Python
Cassandra @ Netflix: Monitoring C* at Scale, Gossip and Tickler & PythonCassandra @ Netflix: Monitoring C* at Scale, Gossip and Tickler & Python
Cassandra @ Netflix: Monitoring C* at Scale, Gossip and Tickler & PythonDataStax Academy
 
Cassandra @ Sony: The good, the bad, and the ugly part 1
Cassandra @ Sony: The good, the bad, and the ugly part 1Cassandra @ Sony: The good, the bad, and the ugly part 1
Cassandra @ Sony: The good, the bad, and the ugly part 1DataStax Academy
 
Cassandra @ Sony: The good, the bad, and the ugly part 2
Cassandra @ Sony: The good, the bad, and the ugly part 2Cassandra @ Sony: The good, the bad, and the ugly part 2
Cassandra @ Sony: The good, the bad, and the ugly part 2DataStax Academy
 
Standing Up Your First Cluster
Standing Up Your First ClusterStanding Up Your First Cluster
Standing Up Your First ClusterDataStax Academy
 
Real Time Analytics with Dse
Real Time Analytics with DseReal Time Analytics with Dse
Real Time Analytics with DseDataStax Academy
 
Introduction to Data Modeling with Apache Cassandra
Introduction to Data Modeling with Apache CassandraIntroduction to Data Modeling with Apache Cassandra
Introduction to Data Modeling with Apache CassandraDataStax Academy
 
Enabling Search in your Cassandra Application with DataStax Enterprise
Enabling Search in your Cassandra Application with DataStax EnterpriseEnabling Search in your Cassandra Application with DataStax Enterprise
Enabling Search in your Cassandra Application with DataStax EnterpriseDataStax Academy
 
Advanced Data Modeling with Apache Cassandra
Advanced Data Modeling with Apache CassandraAdvanced Data Modeling with Apache Cassandra
Advanced Data Modeling with Apache CassandraDataStax Academy
 

Mais de DataStax Academy (20)

Forrester CXNYC 2017 - Delivering great real-time cx is a true craft
Forrester CXNYC 2017 - Delivering great real-time cx is a true craftForrester CXNYC 2017 - Delivering great real-time cx is a true craft
Forrester CXNYC 2017 - Delivering great real-time cx is a true craft
 
Introduction to DataStax Enterprise Graph Database
Introduction to DataStax Enterprise Graph DatabaseIntroduction to DataStax Enterprise Graph Database
Introduction to DataStax Enterprise Graph Database
 
Introduction to DataStax Enterprise Advanced Replication with Apache Cassandra
Introduction to DataStax Enterprise Advanced Replication with Apache CassandraIntroduction to DataStax Enterprise Advanced Replication with Apache Cassandra
Introduction to DataStax Enterprise Advanced Replication with Apache Cassandra
 
Cassandra on Docker @ Walmart Labs
Cassandra on Docker @ Walmart LabsCassandra on Docker @ Walmart Labs
Cassandra on Docker @ Walmart Labs
 
Cassandra 3.0 Data Modeling
Cassandra 3.0 Data ModelingCassandra 3.0 Data Modeling
Cassandra 3.0 Data Modeling
 
Cassandra Adoption on Cisco UCS & Open stack
Cassandra Adoption on Cisco UCS & Open stackCassandra Adoption on Cisco UCS & Open stack
Cassandra Adoption on Cisco UCS & Open stack
 
Data Modeling for Apache Cassandra
Data Modeling for Apache CassandraData Modeling for Apache Cassandra
Data Modeling for Apache Cassandra
 
Coursera Cassandra Driver
Coursera Cassandra DriverCoursera Cassandra Driver
Coursera Cassandra Driver
 
Production Ready Cassandra
Production Ready CassandraProduction Ready Cassandra
Production Ready Cassandra
 
Cassandra @ Netflix: Monitoring C* at Scale, Gossip and Tickler & Python
Cassandra @ Netflix: Monitoring C* at Scale, Gossip and Tickler & PythonCassandra @ Netflix: Monitoring C* at Scale, Gossip and Tickler & Python
Cassandra @ Netflix: Monitoring C* at Scale, Gossip and Tickler & Python
 
Cassandra @ Sony: The good, the bad, and the ugly part 1
Cassandra @ Sony: The good, the bad, and the ugly part 1Cassandra @ Sony: The good, the bad, and the ugly part 1
Cassandra @ Sony: The good, the bad, and the ugly part 1
 
Cassandra @ Sony: The good, the bad, and the ugly part 2
Cassandra @ Sony: The good, the bad, and the ugly part 2Cassandra @ Sony: The good, the bad, and the ugly part 2
Cassandra @ Sony: The good, the bad, and the ugly part 2
 
Standing Up Your First Cluster
Standing Up Your First ClusterStanding Up Your First Cluster
Standing Up Your First Cluster
 
Real Time Analytics with Dse
Real Time Analytics with DseReal Time Analytics with Dse
Real Time Analytics with Dse
 
Introduction to Data Modeling with Apache Cassandra
Introduction to Data Modeling with Apache CassandraIntroduction to Data Modeling with Apache Cassandra
Introduction to Data Modeling with Apache Cassandra
 
Cassandra Core Concepts
Cassandra Core ConceptsCassandra Core Concepts
Cassandra Core Concepts
 
Enabling Search in your Cassandra Application with DataStax Enterprise
Enabling Search in your Cassandra Application with DataStax EnterpriseEnabling Search in your Cassandra Application with DataStax Enterprise
Enabling Search in your Cassandra Application with DataStax Enterprise
 
Bad Habits Die Hard
Bad Habits Die Hard Bad Habits Die Hard
Bad Habits Die Hard
 
Advanced Data Modeling with Apache Cassandra
Advanced Data Modeling with Apache CassandraAdvanced Data Modeling with Apache Cassandra
Advanced Data Modeling with Apache Cassandra
 
Advanced Cassandra
Advanced CassandraAdvanced Cassandra
Advanced Cassandra
 

Último

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 

Último (20)

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 

CQL is not SQL: Key differences between CQL and SQL

  • 1. Chicago 2015 CQL: This is not the SQL you are looking for Aaron Ploetz
  • 2. Wait... CQL is not SQL? l CQL3 introduced in Cassandra 1.1. l CQL is beneficial to new users who have a relational background (which is most of us). l However similar, CQL is NOT a direct implementation of SQL. l New users leave themselves open to issues and frustration when they use CQL with SQL-based expectations.
  • 3. $ whoami l Aaron Ploetz l @APloetz l Lead Database Engineer l Using Cassandra since version 0.8. l Contributor to the Cassandra tag on l 2014/15 DataStax MVP for Apache Cassandra
  • 4. 1 SQL features/keywords not present in CQL 2 Differences between CQL and SQL keywords 3 Secondary Indexes 4 Anti-Patterns 5 Questions
  • 5. SQL features/keywords not present in CQL l JOINs l LIKE l Subqueries l Aggregation l Arithmetic l Except for counters and collections.
  • 6. Differences between CQL and SQL keywords l WHERE l PRIMARY KEY l ORDER BY l IN l DISTINCT l COUNT l LIMIT l INSERT vs. UPDATE (“upsert”)
  • 7. WHERE l Only supports AND, IN, =, >, >=, <, <=. l Some only function under certain conditions. l Also: CONTAINS, CONTAINS KEY for indexed collections. l Does not exist: OR, != l Conditions can only operate on PRIMARY KEY components, and in the defined order of the keys.
  • 8. WHERE (cont) l SELECT * FROM shipcrewregistry WHERE shipname='Serenity'; l Start with partition key(s); cannot skip PRIMARY KEY components. l CREATE TABLE shipcrewregistry (shipname text, lastname text, firstname text, citizenid uuid, aliases set<text>, PRIMARY KEY (shipname, lastname, firstname, citizenid));
  • 9. ALLOW FILTERING l Actually I lied, you can skip primary key components if you apply the ALLOW FILTERING clause. l SELECT * FROM shipcrewregistry WHERE lastname='Washburne';
  • 10. ALLOW FILTERING (cont) l SELECT * FROM shipcrewregistry WHERE lastname='Washburne' ALLOW FILTERING; l But I don't recommend that. l ALLOW FILTERING pulls back all rows and then applies your WHERE conditions. l The folks at DataStax have proposed some alternate names... l Bottom line, if you are using ALLOW FILTERING, you are doing it wrong.
  • 11. PRIMARY KEY l PRIMARY KEYs function differently between Cassandra and relational databases. l Cassandra uses primary keys to determine data distribution and on-disk sort order. l Partition keys are the equivalent of “old school” row keys. l Clustering keys determine on-disk sort order within a partitioning key.
  • 12. ORDER BY l One of the most misunderstood aspects of CQL. l Can only order by clustering columns, in the key order of the clustering columns listed in the table definition (CLUSTERING ORDER). l Which means, that you really don't need ORDER BY. l So what does it do? It can reverse the sort direction (ASCending vs. DESCending) of the first clustering column.
  • 13. PRIMARY KEY / ORDER BY Example: Table Definition l CREATE TABLE postsByUserYear l (userid text, year bigint, tag text, posttime timestamp, content text, postid UUID, PRIMARY KEY ((userid, year), posttime, tag)) WITH CLUSTERING ORDER BY (posttime desc, tag asc);
  • 14. PRIMARY KEY / ORDER BY Example: Queries l SELECT * FROM postsByUserYear WHERE userid='2'; l SELECT * FROM postsByUserYear ORDER BY posttime; l SELECT * FROM postsByUserYear WHERE userid='2' AND year=2015 ORDER BY posttime DESC; l SELECT * FROM postsByUserYear WHERE userid='2' AND year=2015 ORDER BY tag;
  • 15. IN l Can only operate on the last partition key and/or the last clustering key. l And only when the first partition/clustering keys are restricted by an equals relation. l Does not perform well...especially with large clusters.
  • 16. Testing IN l CREATE TABLE bladerunners (id text, type text, ts timestamp, name text, data text, PRIMARY KEY (id));
  • 17. Testing IN (cont) l  SELECT * FROM bladerunners WHERE id IN ('B26354','B26354');
  • 18. DISTINCT l Returns a list of the queried partition keys. l Can only operate on partition key column(s). l In Cassandra DISTINCT returns the partition (row) keys, so it is a fairly light operation (relative to the size of the cluster and/or data set). l Whereas in the relational world, DISTINCT is a very resource intensive operation.
  • 19. COUNT l Counts the number of rows returned, dependent on the WHERE clause. l Does not aggregate. l Similar to its RDBMs counterpart. l Can be (inadvertently) restricted by LIMIT. l Resource intensive command; especially because it has to scan each row in the table (which may be on different nodes), and apply the WHERE conditions.
  • 20. Limit l Limits your query to N rows (where N is a positive integer). l SELECT * FROM bladerunners LIMIT 2; l Does not allow you to specify a start point. l You cannot use LIMIT to “page” through your result set.
  • 21. Cassandra “Upserts” l Under the hood, INSERT and UPDATE are treated the same by Cassandra. l Colloquially known as an “Upsert.” l Both INSERT and UPDATE operations require the complete PRIMARY KEY.
  • 22. So why the different syntax? l Flexibility. Some situations call for one or the other. l Counter columns/tables can only be incremented with an UPDATE. l INSERTs can save you some dev time in the application layer if your PRIMARY KEY changes.
  • 23. “Upsert” example l UPDATE bladerunners SET data='This guy is a one-man slaughterhouse.',name='Harry Bryant',ts='2015-03-30 14:47:00-0600',type='Captain' WHERE id='B16442'; l UPDATE bladerunners SET data = 'Drink some for me, huh pal?' WHERE id='B16442';
  • 24. “Upsert” example (cont) l INSERT INTO bladerunners (id, type, ts, data, name) VALUES ('B29591','Blade Runner','2015-03-30 14:34:00-0600','Captain Bryant would like a word.','Eduardo Gaff'); l INSERT INTO bladerunners (id,data) VALUES ('B29591','It''s too bad she won't live. But then again, who does?');
  • 25. Secondary Indexes l Cassandra provides secondary indexes to allow queries on non- partition key columns. l In 2.1.x you can even create indexes on collections and user defined types. l Designed for convenience, not for performance. l Does not perform well on high-cardinality columns. l Extremely low cardinality is also not a good idea. l Low performance on a frequently updated column. l In my opinion, try to avoid using them all together.
  • 26. Anti-Patterns l Multi-Key queries: IN l Secondary Index queries l DELETEs or INSERTing null values
  • 27. Summary l While CQL is designed to make use of our previous experience using SQL, it is important to remember that the two do not behave the same. l Even if you are at an expert level in SQL, read the CQL documentation before making any assumptions.
  • 28. Additional Reading l Getting Started with Time Series Data Modeling – Patrick McFadin l SELECT – DataStax CQL 3.1 documentation l Counting Keys in Cassandra – Richard Low l Cassandra High Availability – Robbie Strickland