Cassandra querying without full primary key












1















As far as I understand there are no secondary indices with multiple columns, only single columns.



So how do I solve the index problem I'm having? This is my table:



CREATE TABLE IF NOT EXISTS logstv.messages (
timestamp timestamp,
channelid bigint,
userid bigint,
message text,
PRIMARY KEY ((channelid, timestamp), userid)
) WITH CLUSTERING ORDER BY (timestamp DESC);


But this is the error currently:
Missing CLUSTERING ORDER for column userid



I want these things:




  • SELECT WHERE with channelid, userid, timestamp with an order by timestamp and limit


  • SELECT WHERE with channelid, timestamp with an order by timestamp and limit



Is this possible?



The only option I currently see is setting timestamp as the primary key and then sorting through the rest in my software, which seems very expensive. Or having 2 tables for my data which i want to very much avoid because the data will grow very fast and get big.



Example queries:



SELECT message, timestamp
FROM logstv.messages
WHERE userid = ?
AND channelid = ?
AND timestamp >= ?
AND timestamp <= ?
ORDER BY timestamp DESC
LIMIT 100;

SELECT message, timestamp
FROM logstv.messages
WHERE channelid = ?
AND timestamp >= ?
AND timestamp <= ?
ORDER BY timestamp DESC
LIMIT 100;









share|improve this question
















bumped to the homepage by Community 9 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.




















    1















    As far as I understand there are no secondary indices with multiple columns, only single columns.



    So how do I solve the index problem I'm having? This is my table:



    CREATE TABLE IF NOT EXISTS logstv.messages (
    timestamp timestamp,
    channelid bigint,
    userid bigint,
    message text,
    PRIMARY KEY ((channelid, timestamp), userid)
    ) WITH CLUSTERING ORDER BY (timestamp DESC);


    But this is the error currently:
    Missing CLUSTERING ORDER for column userid



    I want these things:




    • SELECT WHERE with channelid, userid, timestamp with an order by timestamp and limit


    • SELECT WHERE with channelid, timestamp with an order by timestamp and limit



    Is this possible?



    The only option I currently see is setting timestamp as the primary key and then sorting through the rest in my software, which seems very expensive. Or having 2 tables for my data which i want to very much avoid because the data will grow very fast and get big.



    Example queries:



    SELECT message, timestamp
    FROM logstv.messages
    WHERE userid = ?
    AND channelid = ?
    AND timestamp >= ?
    AND timestamp <= ?
    ORDER BY timestamp DESC
    LIMIT 100;

    SELECT message, timestamp
    FROM logstv.messages
    WHERE channelid = ?
    AND timestamp >= ?
    AND timestamp <= ?
    ORDER BY timestamp DESC
    LIMIT 100;









    share|improve this question
















    bumped to the homepage by Community 9 mins ago


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.


















      1












      1








      1








      As far as I understand there are no secondary indices with multiple columns, only single columns.



      So how do I solve the index problem I'm having? This is my table:



      CREATE TABLE IF NOT EXISTS logstv.messages (
      timestamp timestamp,
      channelid bigint,
      userid bigint,
      message text,
      PRIMARY KEY ((channelid, timestamp), userid)
      ) WITH CLUSTERING ORDER BY (timestamp DESC);


      But this is the error currently:
      Missing CLUSTERING ORDER for column userid



      I want these things:




      • SELECT WHERE with channelid, userid, timestamp with an order by timestamp and limit


      • SELECT WHERE with channelid, timestamp with an order by timestamp and limit



      Is this possible?



      The only option I currently see is setting timestamp as the primary key and then sorting through the rest in my software, which seems very expensive. Or having 2 tables for my data which i want to very much avoid because the data will grow very fast and get big.



      Example queries:



      SELECT message, timestamp
      FROM logstv.messages
      WHERE userid = ?
      AND channelid = ?
      AND timestamp >= ?
      AND timestamp <= ?
      ORDER BY timestamp DESC
      LIMIT 100;

      SELECT message, timestamp
      FROM logstv.messages
      WHERE channelid = ?
      AND timestamp >= ?
      AND timestamp <= ?
      ORDER BY timestamp DESC
      LIMIT 100;









      share|improve this question
















      As far as I understand there are no secondary indices with multiple columns, only single columns.



      So how do I solve the index problem I'm having? This is my table:



      CREATE TABLE IF NOT EXISTS logstv.messages (
      timestamp timestamp,
      channelid bigint,
      userid bigint,
      message text,
      PRIMARY KEY ((channelid, timestamp), userid)
      ) WITH CLUSTERING ORDER BY (timestamp DESC);


      But this is the error currently:
      Missing CLUSTERING ORDER for column userid



      I want these things:




      • SELECT WHERE with channelid, userid, timestamp with an order by timestamp and limit


      • SELECT WHERE with channelid, timestamp with an order by timestamp and limit



      Is this possible?



      The only option I currently see is setting timestamp as the primary key and then sorting through the rest in my software, which seems very expensive. Or having 2 tables for my data which i want to very much avoid because the data will grow very fast and get big.



      Example queries:



      SELECT message, timestamp
      FROM logstv.messages
      WHERE userid = ?
      AND channelid = ?
      AND timestamp >= ?
      AND timestamp <= ?
      ORDER BY timestamp DESC
      LIMIT 100;

      SELECT message, timestamp
      FROM logstv.messages
      WHERE channelid = ?
      AND timestamp >= ?
      AND timestamp <= ?
      ORDER BY timestamp DESC
      LIMIT 100;






      index cassandra






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 24 '18 at 15:59







      gempir

















      asked Sep 23 '18 at 14:40









      gempirgempir

      1064




      1064





      bumped to the homepage by Community 9 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community 9 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Yeah, It is possible with 'ALLOW FILTERING' option. BUT NOT ADVISABLE (well explained in this link). Schema design in Cassandra is majorly Query Specific (i.e) design your schema so that it is able to answer a query.



          If you want to query on different attributes, go for individual schemas for each one.






          share|improve this answer
























          • Is it maybe possible without ALLOW FILTERING; but only supporting WHERE timestamp + channelid and timestamp + userid + channelid ? Leaving out some queries?

            – gempir
            Sep 24 '18 at 14:19













          • @gempir as per your model in the question, you can query only based on channel.id + user.id /// or /// channel.id+user.id+timestamp and nothing more than that .

            – Dinesh Kumar
            Sep 24 '18 at 14:45













          • The schema is changeable. I just want to avoid storing messages twice in 2 tables

            – gempir
            Sep 24 '18 at 14:51











          • What kind of queries do you want to apply on logstv.messages?

            – Dinesh Kumar
            Sep 24 '18 at 14:58











          • 2 kind something with channelid, timestamp and userid AND a query with just channelid and timestamp

            – gempir
            Sep 24 '18 at 15:09











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "182"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f218354%2fcassandra-querying-without-full-primary-key%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Yeah, It is possible with 'ALLOW FILTERING' option. BUT NOT ADVISABLE (well explained in this link). Schema design in Cassandra is majorly Query Specific (i.e) design your schema so that it is able to answer a query.



          If you want to query on different attributes, go for individual schemas for each one.






          share|improve this answer
























          • Is it maybe possible without ALLOW FILTERING; but only supporting WHERE timestamp + channelid and timestamp + userid + channelid ? Leaving out some queries?

            – gempir
            Sep 24 '18 at 14:19













          • @gempir as per your model in the question, you can query only based on channel.id + user.id /// or /// channel.id+user.id+timestamp and nothing more than that .

            – Dinesh Kumar
            Sep 24 '18 at 14:45













          • The schema is changeable. I just want to avoid storing messages twice in 2 tables

            – gempir
            Sep 24 '18 at 14:51











          • What kind of queries do you want to apply on logstv.messages?

            – Dinesh Kumar
            Sep 24 '18 at 14:58











          • 2 kind something with channelid, timestamp and userid AND a query with just channelid and timestamp

            – gempir
            Sep 24 '18 at 15:09
















          0














          Yeah, It is possible with 'ALLOW FILTERING' option. BUT NOT ADVISABLE (well explained in this link). Schema design in Cassandra is majorly Query Specific (i.e) design your schema so that it is able to answer a query.



          If you want to query on different attributes, go for individual schemas for each one.






          share|improve this answer
























          • Is it maybe possible without ALLOW FILTERING; but only supporting WHERE timestamp + channelid and timestamp + userid + channelid ? Leaving out some queries?

            – gempir
            Sep 24 '18 at 14:19













          • @gempir as per your model in the question, you can query only based on channel.id + user.id /// or /// channel.id+user.id+timestamp and nothing more than that .

            – Dinesh Kumar
            Sep 24 '18 at 14:45













          • The schema is changeable. I just want to avoid storing messages twice in 2 tables

            – gempir
            Sep 24 '18 at 14:51











          • What kind of queries do you want to apply on logstv.messages?

            – Dinesh Kumar
            Sep 24 '18 at 14:58











          • 2 kind something with channelid, timestamp and userid AND a query with just channelid and timestamp

            – gempir
            Sep 24 '18 at 15:09














          0












          0








          0







          Yeah, It is possible with 'ALLOW FILTERING' option. BUT NOT ADVISABLE (well explained in this link). Schema design in Cassandra is majorly Query Specific (i.e) design your schema so that it is able to answer a query.



          If you want to query on different attributes, go for individual schemas for each one.






          share|improve this answer













          Yeah, It is possible with 'ALLOW FILTERING' option. BUT NOT ADVISABLE (well explained in this link). Schema design in Cassandra is majorly Query Specific (i.e) design your schema so that it is able to answer a query.



          If you want to query on different attributes, go for individual schemas for each one.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Sep 23 '18 at 19:55









          Dinesh KumarDinesh Kumar

          153113




          153113













          • Is it maybe possible without ALLOW FILTERING; but only supporting WHERE timestamp + channelid and timestamp + userid + channelid ? Leaving out some queries?

            – gempir
            Sep 24 '18 at 14:19













          • @gempir as per your model in the question, you can query only based on channel.id + user.id /// or /// channel.id+user.id+timestamp and nothing more than that .

            – Dinesh Kumar
            Sep 24 '18 at 14:45













          • The schema is changeable. I just want to avoid storing messages twice in 2 tables

            – gempir
            Sep 24 '18 at 14:51











          • What kind of queries do you want to apply on logstv.messages?

            – Dinesh Kumar
            Sep 24 '18 at 14:58











          • 2 kind something with channelid, timestamp and userid AND a query with just channelid and timestamp

            – gempir
            Sep 24 '18 at 15:09



















          • Is it maybe possible without ALLOW FILTERING; but only supporting WHERE timestamp + channelid and timestamp + userid + channelid ? Leaving out some queries?

            – gempir
            Sep 24 '18 at 14:19













          • @gempir as per your model in the question, you can query only based on channel.id + user.id /// or /// channel.id+user.id+timestamp and nothing more than that .

            – Dinesh Kumar
            Sep 24 '18 at 14:45













          • The schema is changeable. I just want to avoid storing messages twice in 2 tables

            – gempir
            Sep 24 '18 at 14:51











          • What kind of queries do you want to apply on logstv.messages?

            – Dinesh Kumar
            Sep 24 '18 at 14:58











          • 2 kind something with channelid, timestamp and userid AND a query with just channelid and timestamp

            – gempir
            Sep 24 '18 at 15:09

















          Is it maybe possible without ALLOW FILTERING; but only supporting WHERE timestamp + channelid and timestamp + userid + channelid ? Leaving out some queries?

          – gempir
          Sep 24 '18 at 14:19







          Is it maybe possible without ALLOW FILTERING; but only supporting WHERE timestamp + channelid and timestamp + userid + channelid ? Leaving out some queries?

          – gempir
          Sep 24 '18 at 14:19















          @gempir as per your model in the question, you can query only based on channel.id + user.id /// or /// channel.id+user.id+timestamp and nothing more than that .

          – Dinesh Kumar
          Sep 24 '18 at 14:45







          @gempir as per your model in the question, you can query only based on channel.id + user.id /// or /// channel.id+user.id+timestamp and nothing more than that .

          – Dinesh Kumar
          Sep 24 '18 at 14:45















          The schema is changeable. I just want to avoid storing messages twice in 2 tables

          – gempir
          Sep 24 '18 at 14:51





          The schema is changeable. I just want to avoid storing messages twice in 2 tables

          – gempir
          Sep 24 '18 at 14:51













          What kind of queries do you want to apply on logstv.messages?

          – Dinesh Kumar
          Sep 24 '18 at 14:58





          What kind of queries do you want to apply on logstv.messages?

          – Dinesh Kumar
          Sep 24 '18 at 14:58













          2 kind something with channelid, timestamp and userid AND a query with just channelid and timestamp

          – gempir
          Sep 24 '18 at 15:09





          2 kind something with channelid, timestamp and userid AND a query with just channelid and timestamp

          – gempir
          Sep 24 '18 at 15:09


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Database Administrators Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f218354%2fcassandra-querying-without-full-primary-key%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          ف. موراي أبراهام

          صرب

          كأس إنترتوتو