20 Ocak 2022 Perşembe

INSERT INTO + SELECT

Örnek
Şöyle yaparız
INSERT INTO suppliers (supplierName)
  SELECT customerName FROM customers WHERE country = 'USA' AND state = 'CA';
Örnek
Şöyle yaparız
INSERT INTO stats(totalProduct, totalCustomer, totalOrder)
VALUES(
  (SELECT COUNT(*) FROM products),
  (SELECT COUNT(*) FROM customers),
  (SELECT COUNT(*) FROM orders)
);
Örnek 
 Ben şöyle yapmıştım. Böylece phone alanına sabit bir değer verebilmiştim
INSERT INTO suppliers supplierName, phone
  SELECT customerName, 12345 FROM customers WHERE country = 'USA' AND state = 'CA';


19 Ocak 2022 Çarşamba

LIMIT

Giriş
Oracle'daki ROWNUM gibidir. Sonuç listesini kısaltır ancak sonuç listesine sıralama değerini eklemez. Sıralama değeri için MySQL'deki ROW_NUM yazısına bakınız. Açıklaması şöyle
LIMIT is not a standard SQL clause. Other SQL dialects have variations of LIMIT. For example, MS-SQL Server uses TOP, and Oracle uses ROWNUM.
Limit ile sabit bir sayı kullanılmalıdır. Açıklaması şöyle
The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except when using prepared statements).
Limit 0'dan başlar. Üç sayfayı çekmek için şöyle yaparız
SELECT ... LIMIT 0, 10; -- İlk 10 kayıt
SELECT ... LIMIT 10, 10;-- Sonraki 10 kayıt
SELECT ... LIMIT 20, 10; -- Sonraki 10 kayıt
Örnek
Şöyle yaparız
SELECT column_name(s) FROM table_name WHERE condition LIMIT number;
2. Sayfalama
İki çeşit sayfalama yöntemi var. Açıklaması şöyle
When it comes to pagination, two of the most commonly used algorithms are offset limit and cursor pagination.
1. OFFSET sayfalama : WHERE koşulu içermez
2. Cursor veya Seek sayfalama : WHERE koşulu içerir
Farkları şöyle
// Offset pagination
SELECT * FROM users ORDER BY id DESC
LIMIT 10 OFFSET 20;

// Cursor pagination
SELECT * FROM users WHERE id <= 1234 ORDER BY id DESC LIMIT 10
OFFSET'in açıklaması şöyle. Kaç tane satırın atlanacağını belirtir.
LIMIT n is an alternative syntax to the FETCH FIRST n ROWS ONLY.

The OFFSET clause specifies the number of rows of the result table to skip before any rows are retrieved, and must be used with the LIMIT clause.

The OFFSET clause instructs the server where to start returning rows within the query result.
For example, if a query returns 100 rows, specifying OFFSET 10 instructs the server to skip
the first 10 rows of the query results:

SELECT *
FROM PART_DI21PART_STAININFO
ORDER BY record_id
LIMIT 100 OFFSET 10

OFFSET can also be abbreviated, e.g.
LIMIT 100,10
Eğer OFFSET değeri büyükse sorgu yavaş kalabilir. Açıklaması şöyle. Yani satırları atlamak için olsa bile veri tabanı yine de bazı satırları yüklemek zorunda
One of the significant differences between them is that offset pagination can be much slower when dealing with large offset.
...
Every record in a database is stored on a data page and ordered physically according to the clustered index.

Each data page contains different number of rows depending on the row size. When performing an OFFSET LIMIT query, the database has no idea which pages the first set of rows reside on.

As a result, the database retrieves OFFSET + LIMIT number of rows and removes the unnecessary data in memory.

As the offset increases, the database needs to query more data, hence affecting the performance.

Cursor pagination, on the other hand, locates the data in an index tree precisely, avoiding the need to go through unnecessary data. Hence, its performance stays consistent regardless of the offset size.
Benzer bir açıklama şöyle
Using OFFSET, even on the primary key, becomes slower over time for high database volume, as the number of rows that need to be loaded in order to be skipped becomes higher and higher.
İndeksli Olan Sütun İçin Sayfalama
Açıklaması şöyle. Primary Key değil de indeks kullansa da satırları atlamak için olsa bile veri tabanı yine de bazı satırları yüklemek zorunda
Similarly, when paginating using a non-clustered index, the database retrieves OFFSET + LIMIT number of data in the non-clustered index tree and removes any extra data in memory.

In the example above, the queried data — id and user_id, both exist in the non-clustered index tree. Hence, the database doesn’t need to do an additional look up in the clustered index tree.

This is also known as a covering index scan.


If the required columns are not in the non-clustered index tree (we are querying all columns in the example above), the database needs to do an additional lookup in the clustered index tree, which incurs extra I/Os.

As the offset increases, the query optimiser may decide that a full table scan is more efficient, which can be slow for large tables.
İndeksli Olmayan Sütun İçin Sayfalama
Açıklaması şöyle
Using offset on a non-indexed sorted column is a heavy task for the database and must be avoided at all costs.
Benzer bir açıklama şöyle. Yani hem OFFSET hem de CURSOR sayfalama kötü sonuç veriyor
As you may have guessed, when paginating using a non-indexed column, both offset limit and cursor pagination has the same performance.

Both algorithms require a full table scan, sort and limit operation which can be slow and inefficient.

Hence, it’s important to consider adding an index on the column you’re using for pagination to improve performance.
age sütununda indeks olmasın. Şu ikisi de kötü sonuç verir
// Offset pagination
SELECT * FROM users ORDER BY age DESC
LIMIT 10 OFFSET 20;

// Cursor pagination
SELECT * FROM users WHERE age <= 10 ORDER BY age DESC LIMIT 10
Örnek - OFFSET
Şöyle yaparız. Bu yazıda sayfalama (pagination) için OFFSET kullanımının veri tabanına yük getirdiği de gösteriliyor
SELECT * FROM objects ORDER BY id LIMIT 3 OFFSET 100;
Bunun yerine Seek method pagination kullanılabilir. Şöyle yaparız. Burada sayfadaki en son satırın özelliğinden daha büyük olan sonraki satır isteniyor.
SELECT * FROM objects WHERE id >= 100 ORDER BY id LIMIT 3;



3 Ocak 2022 Pazartesi

JDBC Sürücüleri

MariaDB
Şöyle yaparız
<dependency>
  <groupId>org.mariadb.jdbc</groupId>
  <artifactId>mariadb-java-client</artifactId>
  <version>2.7.4</version>
</dependency>
MySQL
JDBC Sürücüleri - MySQL yazısına taşıdım

MariaDB Connection String
Şöyle yaparız
jdbc:mariadb://localhost:3306/jdbc_demo
MySQL Connection String
MySQL Connection String yazısına taşıdım

MySQL JDBC Prepared Statement Caching
Prepared Statement nesneleri MySQL sürücüsü tarafından istemci tarafında ön bellekte tutuluyor. Bir yazı burada


mariadb komutu

Giriş
mariadb komutu ile mysql komutu çok benziyorlar

-h seçeneği
Şöyle yaparız
mariadb --host example.skysql.net --port 5001 \
    --user db_user --password \
    --ssl-verify-server-cert \
    --ssl-ca /path/to/skysql_chain.pem
-u seçeneği
Şöyle yaparız
mariadb -u user -p

Soft Delete

Giriş Açıklaması  şöyle When using the soft delete mechanism on the database, you might run into a situation where a record with a unique co...