29 Ağustos 2023 Salı

JDBC DatabaseMetaData.getTables metodu

Giriş
Bir makale burada. MySQL açısından bir veri tabanı şeklen şöyle
db 			<-- Ben yarattım
information_schema
mysql                   <-- MySQL kurulumu ile geliyor
performance_schema
sys
Açıklaması şöyle
The MySQL installation comes with default databases. One of the database is named 'mysql'.

db veri tabanını yaratmak için şöyle yaptım
CREATE TABLE IF NOT EXISTS db.myworker (...);
Örnek - catalog değeri geç
Elimizde şöyle bir kod olsun
try (Connection connection = ...;
  ResultSet tables = connection.getMetaData().getTables( connection.getCatalog(),
        null,
        null,
        new String[]{"TABLE", "VIEW"})) {
  ...
}
1. Eğer bağlantı şöyle ise yani veri tabanı belirtilmemişse 
jdbc:mysql://localhost:3306/
connection.getCatalog() çağrısı "" döner
connection.getSchema() çağrısı null döner
ResultSet te boş geliyor


2. Eğer bağlantı şöyle ise yani veri tabanı belirtilmişse
jdbc:mysql://localhost:3306/db
connection.getCatalog() çağrısı db döner
connection.getSchema() çağrısı null döner
ResultSet dolu gelir ve içinde sadece db veri tabanında yarattığım tabloları görürüm

Örnek - catalog null yani catalog değeri geçme
Elimizde şöyle bir kod olsun. Bu sefer catalog yerine null geçtim. 
try (Connection connection = ...;
  ResultSet tables = connection.getMetaData().getTables( null,
        null,
        null,
        new String[]{"TABLE", "VIEW"})) {
  ...
}
1. Eğer bağlantı şöyle ise yani veri tabanı belirtilmemişse 
jdbc:mysql://localhost:3306/
connection.getCatalog() çağrısı "" döner
connection.getSchema() çağrısı null döner
ResultSet  dolu gelir ve içinde sys ve db dahil her şeyi görürüm. Çünkü catalog değeri olarak null geçtim

2. Eğer bağlantı şöyle ise yani veri tabanı belirtilmişse
jdbc:mysql://localhost:3306/db
connection.getCatalog() çağrısı db döner
connection.getSchema() çağrısı null döner
ResultSet  dolu gelir ve içinde sys ve db dahil her şeyi görürüm. Çünkü catalog değeri olarak null geçtim

21 Ağustos 2023 Pazartesi

EXPLAIN ANALYZE

Table Scan
Full Scan anlamına gelir. 
Örnek
Şöyle yaparız. id alanında index veya primary key olmadığı için tüm tabloyu tarar
mysql> explain analyze select * from employee1 where id = 3456;
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| EXPLAIN                                                                                                                                                                                               |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| -> Filter: (employee1.id = 3456)  (cost=1989 rows=1965) (actual time=5.24..29.3 rows=1 loops=1)
    -> Table scan on employee1  (cost=1989 rows=19651) (actual time=0.0504..27.3 rows=20000 loops=1)
 |
+--------------------------------------------------------------------------------------------------------
Index lookup
Örnek
Tablo için bir indeks yaratalım
CREATE INDEX index1 ON employee1 (FirstName);
Şöyle yaparız. FirstName alanında index olduğu için tüm indeksi tarar
mysql> explain analyze select * from employee1 where FirstName = 'user-13456';
+-------------------------------------------------------------------------------------------------------------------------------------+
| EXPLAIN                                                                                                                             |
+-------------------------------------------------------------------------------------------------------------------------------------+
| -> Index lookup on employee1 using index1 (FirstName='user-13456')  (cost=0.35 rows=1) (actual time=0.0594..0.0669 rows=1 loops=1)
 |
+-------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

SHOW INDEXES

Örnek
Şöyle yaparız
mysql> SHOW INDEXES FROM employee2 \G;
*************************** 1. row ***************************
        Table: employee2
   Non_unique: 0
     Key_name: PRIMARY
 Seq_in_index: 1
  Column_name: id
    Collation: A
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: 
   Index_type: BTREE
      Comment: 
Index_comment: 
      Visible: YES
   Expression: NULL
1 row in set (0.00 sec)

SET GLOBAL

Örnek
Şöyle yaparız
mysql> SET GLOBAL validate_password.policy = 0;
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

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...