Welcome to this week’s tips and tricks blog where we recap some distributed SQL questions from around the Internet. We’ll also review upcoming events, new documentation, and blogs that have been published since the last post. Got questions? Make sure to ask them on our YugabyteDB Slack channelForumGitHub, or Stackoverflow. Ok, let’s dive right in:

How to use joins in UPDATE statements

Sometimes, you need to update rows in a table based on values in another table. In this case, you can use the PostgreSQL UPDATE join syntax with the FROM clause as follows.

First we create a table of products and product segments:

yugabyte=## CREATE TABLE product_segment(id BIGINT PRIMARY KEY, name TEXT NOT NULL, discount NUMERIC (4,2));
yugabyte=## INSERT INTO product_segment (id,name,discount) VALUES (1, 'Luxury', 0.06),(2, 'Mass', 0.1);
yugabyte=## SELECT * FROM product_segment;
 id |  name  | discount
----+--------+----------
  2 | Mass   | 	0.10
  1 | Luxury | 	0.06
(2 rows)
yugabyte=## CREATE TABLE product (id BIGINT PRIMARY KEY, name TEXT NOT NULL, price NUMERIC(10,2), net_price NUMERIC(10,2), segment_id BIGINT NOT NULL);
yugabyte=## INSERT INTO product(id,name,price,segment_id) VALUES (1, 'fancy clothes', 199, 1), (2, 'tshirt', 3.50, 2);
yugabyte=## SELECT * FROM product;
 id |     name      | price  | net_price | segment_id 
----+---------------+--------+-----------+------------
  2 | tshirt        |   3.50 |           |          2
  1 | fancy clothes | 199.00 |           |          1
(2 rows)

Now we want to set the net_price of products based on the discount of the product_segment that they belong to.

We can use the FROM clause to join other tables when we’re updating the base table:

yugabyte=## UPDATE product SET net_price = price - price * discount FROM product_segment WHERE product.segment_id = product_segment.id;

Then we can query the products to see the updated rows:

yugabyte=## SELECT * FROM product;
 id |     name      | price  | net_price | segment_id 
----+---------------+--------+-----------+------------
  2 | tshirt        |   3.50 |      3.15 |          2
  1 | fancy clothes | 199.00 |    187.06 |          1
(2 rows)

Does YugabyteDB support namespaces and can multiple applications use a cluster without interfering with each other?

For YSQL, YugabyteDB supports the PostgreSQL notion of database as a namespace mechanism. [Role-based access control](https://docs.yugabyte.com/latest/api/ysql/commands/dcl_create_role/) can be used to limit user access to these databases using the GRANT command.

Another way is to use multiple schemas in a database to support multiple tenants in one database. This way you can have multiple applications each having its own database, and multiple tenants in an application each having its own data.

For YCQL, YugabyteDB supports the Apache Cassandra like notion of keyspace. And there’s a corresponding role-based access control mechanism supported by the GRANT PERMISSION command.

How to run large scans in YSQL

PostgreSQL doesn’t have any query timeouts by default. While this may be OK in single node databases, in a cluster environment a bad query may end up slowing down your whole cluster. Therefore we have enabled query timeouts of 60000 milliseconds (60 seconds) by default.

Sometimes you have queries that should run for longer than that. The timeout can be increased by setting the statement_timeout parameter in YSQL to a bigger value. This can be set when you’re sending queries from your application driver or in the ysqlsh CLI for example:

yugabyte=## set statement_timeout = 180000;
yugabyte=## BEGIN ISOLATION LEVEL SERIALIZABLE, READ ONLY, DEFERRABLE;
yugabyte=## <some long SELECT query>

Using ISOLATION LEVEL SERIALIZABLE, READ ONLY, DEFERRABLE also helps long running queries by ensuring that the read snapshot used is consistent with later database states by waiting for a point in the transaction stream at which no anomalies can be present, so that there is no risk of the query failing due to conflicts from concurrent transactions.

#databases #distributed sql #how to #postgresql

Distributed SQL Tips and Tricks - Oct 16, 2020
1.65 GEEK