26 Ekim 2022 Çarşamba

CREATE PROCEDURE İle Tabloyu Doldurmak

Örnek
Örneği burada aldım. Bir tabloyu doldurmak için şöyle yaptım
CREATE PROCEDURE myproc()
BEGIN
  DECLARE i INT DEFAULT 1; 
  WHILE i <= 1000000 DO
    INSERT INTO dbmap (id,name,ssn) VALUES (i,CONCAT('name',i),CONCAT('SSN',i));
    SET i = i+1;
  END WHILE;
END
Çağırmak için şöyle yaptım
call  myproc()
Örnek
Bir tabloyu doldurmak için şöyle yaptım
CREATE PROCEDURE myproc(IN NumRows INT)
BEGIN DECLARE i INT DEFAULT 1; WHILE i <= NumRows DO insert into dbmap (id,name,ssn) values (i,CONCAT('name', i), CONCAT('ssn' , i)); SET i = i+1; END WHILE; END
Çağırmak için şöyle yaptım
CALL myproc(100000)
Örnek
Bir tabloyu doldurmak için şöyle yaparız
CREATE PROCEDURE testdb.BulkInsert()
BEGIN
  DECLARE i INT DEFAULT 1;
  truncate table employee1;
  truncate table employee2;
  WHILE (i <= 20000) DO
    INSERT INTO testdb.employee1 (id, FirstName, Address) 
      VALUES(i, CONCAT("user","-",i), CONCAT("address","-",i));
    INSERT INTO testdb.employee2 (id,FirstName, Address) 
      VALUES(i,CONCAT("user","-",i), CONCAT("address","-",i));    
   SET i = i+1;
END WHILE;
END

mysql> CALL testdb.BulkInsert() ;

mysqlsh komutu - MySQL Shell İçindir

Giriş
Kurulum sayfası burada. MySQL'e komut satırından erişmek için kolay bir araç. Eğer daha genel amaçlı bir araç istiyorsak sqlline kullanılabilir

Kurulum
MAC OS X için şöyle yaparız
$ brew install mysql-shell
Bağlanmak için şöyle yaparız
$ mysqlsh --host=127.0.01 --port=3306 --user=root --password=root
Shell komutlarını çalıştırmak için başına \ karakterini koymak lazım

Örnek
Bağlandıktan sonra schema seçmek için şöyle yaparız. Burada db isimli schema'ya geçiyorum.
\ use db
SQL çalıştırmak için SQL moduna geçmek lazım. Şöyle yaparız
\ sql
Sonra şöyle yaparız
select max(id) from dbmap;


    

24 Ekim 2022 Pazartesi

JDBC MySQL Connection String

Örnek
Şöyle yaparız
jdbc:mysql://localhost:3306/jdbc_demo
Örnek
user ve password için Şöyle yaparız
jdbc:mysql://localhost:3306/mydatabase?user=myusername&password=mypassword
Örnek - Multi-Host Connections
Failover için şöyle yaparız
jdbc:mysql://192.168.29.20:3306,192.168.29.20:6306/adv
Açıklaması şöyle
The host list in the connection URL comprises of two types of hosts, the primary and the secondary. When starting a new connection, the driver always tries to connect to the primary host first and, if required, fails over to the secondary hosts on the list sequentially when communication problems are experienced. Even if the initial connection to the primary host fails and the driver gets connected to a secondary host, the primary host never loses its special status: for example, it can be configured with an access mode distinct from those of the secondary hosts, and it can be put on a higher priority when a host is to be picked during a failover process.
autoReconnect
Açıklaması şöyle
autoReconnect=true in the connection string, which makes the connection to auto recover.
Şöyle yaparız.
"jdbc:mysql://localhost:yourPortNumber/yourDatabaseName?autoReconnect=true";
logger
Şöyle yaparız. Burada logger olarak SLF4J kullanılmak isteniyor.
jdbc:mysql://myexample.com:3306/pets?logger=Slf4JLogger&explainSlowQueries=true
serverTimezone
Örnek
Şöyle yaparız
jdbc:mysql://...
  &useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
sql_mode
SQL içindeki ANSI çift tırnakları kabul etmek için şöyle yaparız
jdbc:mysql://localhost:3306/db?sessionVariables=sql_mode=ANSI_QUOTES
Böylece şöyle SQL'ler işlenebilir.
SELECT *  FROM "account" WHERE "deleted" IS NULL
ORDER BY "account_name"
socketTimeout
Açıklaması şöyle. Yani 
- connectTimeout veri tabanına bağlanmak için ne kadar beklemek istediğimizi belirtir. 
- socketTimeout okuma, yazma gibi işlemlerde sokette ne kadar sürede bir veri olması istediğimizi belirtir.
connectTimeout
Default value: 0
Unit: millisecond
Configure the connection timeout time, through the connect(SocketAddress endpoint, int timeout) method of the Socket object to configure

socketTimeout
Default value: 0
Unit: ms
Configure the timeout time of the socket, and configure it through the setSoTimeout(int timeout) method of the Socket object
Şöyle yaparız. Burada her ikisi için de 1 dakika kullanılıyor
jdbc:mysql://xxx.xx.xxx.xxx:3306/database?connectTimeout=60000&socketTimeout=60000
socketTimeout çıktısı şöyle
Caused by: java.net.SocketTimeoutException: Read timed out
  at java.net.SocketInputStream.socketRead0(Native Method)
  at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
  at java.net.SocketInputStream.read(SocketInputStream.java:171)
  at java.net.SocketInputStream.read(SocketInputStream.java:141)
  at com.mysql.jdbc.util.ReadAheadInputStream.fill(ReadAheadInputStream.java:101)
  at com.mysql.jdbc.util.ReadAheadInputStream.readFromUnderlyingStreamIfNecessary(ReadAheadInputStream.java:144)
  at com.mysql.jdbc.util.ReadAheadInputStream.read(ReadAheadInputStream.java:174)
  at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:3008)
  at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3469)
useSSL
Şöyle yaparız. Burada bağlantıda SSL kullanılmaz
"jdbc:mysql://localhost:yourPortNumber/yourDatabaseName?autoReconnect=true&useSSL=false";
useUnicode
Şöyle yaparız
"jdbc:mysql://127.0.0.1:3306/mydb?
  useUnicode=true&
  characterEncoding=UTF-8&
  useServerPrepStmts=false&
  rewriteBatchedStatements=true"
rewriteBatchedStatements
Açıklaması şöyle
By default the MySQL driver does not support batching because of how it handles queries. To enable batching we have to tell it to rewrite queries by adding ?rewriteBatchedStatements=true to your JDBC URL.
useCursorFetch
Açıklaması şöyle
In MySQL, the useCursorFetch parameter is a JDBC driver-specific option that can be used to enable or disable cursor-based fetching for result sets. Cursor-based fetching allows fetching rows from the result set in smaller chunks (cursors) instead of loading the entire result set into memory at once.

When useCursorFetch is set to true, the MySQL JDBC driver uses server-side cursors to fetch rows in chunks. This can be beneficial when dealing with large result sets as it reduces memory consumption and can improve performance.

Here's an example of how to enable useCursorFetch in a MySQL JDBC connection string:

String jdbcUrl = "jdbc:mysql://your_host:your_port/your_database?useCursorFetch=true";

In the JDBC connection URL, appending ?useCursorFetch=true at the end enables the cursor-based fetching mode for the MySQL JDBC driver.













2 Ekim 2022 Pazar

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