Showing posts with label MySQL. Show all posts
Showing posts with label MySQL. Show all posts

Sunday, July 16, 2017

MySQL难点


select ... for update将会锁住任何其他connection的select for update, update, insert。当然还要放入到transaction中。

CREATE DEFINER=`qfu`@`%` PROCEDURE `updateIndex`(IN batchNum INT)
BEGIN

  DROP TEMPORARY TABLE IF EXISTS temp;
  create temporary table temp(id int);
  START TRANSACTION;
  insert into temp
  select id from Dow_Jones_Index2 where isUpdated=0 limit batchNum for update;
  update Dow_Jones_Index2 set isUpdated=1 where id in (select id from temp);
  commit;
  select * from Dow_Jones_Index2 where id in (select id from temp);
  DROP TEMPORARY TABLE IF EXISTS temp;  
END

临时表生命期是session内,如果数据库连接断了,临时表也会消失。相当于mssql中的#temp

---
explain select * from Student where id=5;
可以解释是否用了index。
Using where: 没用到index
Using index: index含有所有信息不需要再查表。The difference is that "Using index" doesn't need a lookup from the index to the table, while "Using index condition" sometimes has to.
Using index condition: 需要再查表

clear cache for performance tuning (workbench)
RESET QUERY CACHE;

ref:
select for update

Thursday, May 4, 2017

AWS RDS

Relational Database大家应该比较熟悉,这里主要讲述配置问题

新建一个数据库实例一定要注意security groups,publicly accessible选yes
如果create new security group的话,本地客户端MySql Workbench就可以直接访问,但是它不能其他AWS资源如lambda访问,所以不推荐
这里推荐用default(VPC), 这时候其他AWS可以访问,但本地默认不能访问,这时候就要点击security groups中的default进入EC2的security group,在inbound rules加入TCP 3306(MySql)允许My IP访问即可。


































Tuesday, April 1, 2014

Java连接MySQL (本地+远程)

1. 下载MySQL driver for Java (jar): mysql conector-java.jar
2. 加入到项目的external JARs
2.  Local MySQL是用wamp的服务,而服务器MySQL是godaddy的服务

本地连接:
代码如下:

package rename;

import java.sql.*;

public class Rename {
public static void main(String[] args){

        Connection conn = null;
         try
         {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mymenu", "root", "");
            //System.out.print("Conn Succeed");        
         
            String sql = "select * from dishes";  
            Statement st = (Statement) conn.createStatement();  
            ResultSet rs = st.executeQuery(sql);  

            if(rs.next()) {
                String name = rs.getString("DishName");
                String tags = rs.getString("Tags");
                int count = rs.getInt("ClickCount");
                System.out.println(name + " " + tags + " " + count);
           
            }
            conn.close();
           
         }
         catch (Exception e)
        {
                  System.out.println("Db conn failed" + e.getMessage());
         }

}
}


这个例子是连接本地MySQL的数据库(dbname) mymenu,wamp的用户名默认是root,密码默认为空。然后是连接Table dishes,这个表含有Columns: DishName, Tags, ClickCount.

远程服务器连接:
把上述conn改为:
conn = DriverManager.getConnection("jdbc:mysql://<hostname>:3306/<dbname>", "<username>", "<pw>");

<hostname>也可以为db server的ip