java - 使用 java 循环运行 MySQL

标签 java mysql loops stored-procedures optimization

我有 3 个表格,其中包含以下内容:

Author
idAuthor INT
name VARCHAR

Publication
idPublication INT
Title VARCHAR
Date YEAR
Type VARCHAR
Conference

author_has_publication
author_idAuthor INT
publication_idPublication INT

我正在尝试对作者进行关系模式。目的是显示他们共同发表的出版物数量。作者名字是参数,我最多可以有8个名字。我的代码给出了两位作者之间共同发表的数量,所以我必须循环它。我目前正在使用 Java 循环和 SQL 语句来执行此操作。这是SQL部分

private int runQuery(String a1, String a2){ // a1 author 1 and a2 author 2
        try {
            auth1 = new ArrayList<String>();
            Class.forName("com.mysql.jdbc.Driver");
            Connection connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/mydb", "root", "ROOT");
            Statement stmt = connection.createStatement();
            long start = System.currentTimeMillis();


            String queryUpdate1 = "DROP TABLE IF EXISTS temp1;";
            String queryUpdate2 = "DROP TABLE IF EXISTS temp2;";
            String queryUpdate3 = "CREATE TEMPORARY TABLE IF NOT EXISTS temp1 AS (SELECT Author.name, Publication.idPublication, Publication.title FROM Author INNER JOIN Author_has_Publication ON Author_has_Publication.author_idAuthor=author.idAuthor INNER JOIN Publication ON Author_has_Publication.publication_idPublication=publication.idPublication WHERE Author.name='"+ a1+"');";
            String queryUpdate4 = "CREATE TEMPORARY TABLE IF NOT EXISTS temp2 AS (SELECT Author.name, Publication.idPublication, Publication.title FROM Author INNER JOIN Author_has_Publication ON Author_has_Publication.author_idAuthor=author.idAuthor INNER JOIN Publication ON Author_has_Publication.publication_idPublication=publication.idPublication WHERE Author.name='"+ a2+"');";
            String query = "SELECT COUNT(*) FROM (SELECT temp1.title from temp1 INNER JOIN temp2 on temp1.idPublication = temp2.idPublication) as t;";

            stmt.executeUpdate(queryUpdate1);
            stmt.executeUpdate(queryUpdate2);
            stmt.executeUpdate(queryUpdate3);
            stmt.executeUpdate(queryUpdate4);
            ResultSet rs = stmt.executeQuery(query);
            int result = -1;
            while (rs.next()) {
                result = rs.getInt(1);
            }

            System.out.println("result = " + result);
            long end = System.currentTimeMillis() - start;
            queryTimeLabel.setText("Query Execution Time :"+end);
            connection.close();
            return result;
        } catch (Exception e) {
            System.out.println(e);
        }
        return -1;
    }

这是循环部分(当给出的作者超过 2 位时重复 SQL)并生成图表:

public void actionPerformed(ActionEvent e) {

    graph = new mxGraph();
    Object parent = graph.getDefaultParent();
    authVertex = getAuthors();



    // ///////////////////////////////////
    // CREATES GRAPH, Graph only shows up after you resize the window
    graph.getModel().beginUpdate();
    try {

        int i = 0;
        for(String a: authVertex.keySet()){
            int j = 0;
            for(String b: authVertex.keySet()){
                if(j > i) {
                    graph.insertEdge(parent, null, String.valueOf(runQuery(a,b)), authVertex.get(a), authVertex.get(b)); // loop the SQL statement 2 by 2.
                }
                j++;
            }
            i++;
        }
    } finally {
        graph.getModel().endUpdate();
    }

    graphComponent = new mxGraphComponent(graph);
    graphPan.removeAll();
    graphPan.add(graphComponent);
    setVisible(true);
    // /////////////////////////////////////////


}

我的代码当前正在运行,但我想知道是否可以通过将所有内容传递到 MySQL 来提高性能,这意味着我在参数中输入作者姓名,并且循环由 MySQL 挂起,我检查了MySQL 程序,但我的问题是如何处理作者姓名参数,因为它是一个变量。

最佳答案

一种方式,在一个声明中:

SELECT  COUNT(*)
    FROM  Author_has_Publication AS ap1
    JOIN  Author_has_Publication AS ap2  ON ap1.publication_idPublication =
                                            ap2.publication_idPublication
    JOIN  Author AS a1  ON ap1.author_idAuthor = a1.id_Author
    JOIN  Author AS a2  ON ap2.author_idAuthor = a2.id_Author
    WHERE  a1.name = '...'
      AND  a2.name = '...' 

另一种方式可能是

SELECT  COUNT(*)
    FROM  
    (
        SELECT  ahp.publication_idPublication, COUNT(*)
            FROM  Author_has_Publication AS ahp
            JOIN  Author AS a  ON a.id_Author = ahp.author_idAuthor
            WHERE  a.name IN ('...', '...')
            GROUP BY  ahp.publication_idPublication
            HAVING  COUNT(*) = 2   -- Number of authors
    ) x 

所需的复合索引:

Author_has_Publication:  (author_idAuthor, publication_idPublication)
Author_has_Publication:  (publication_idPublication, author_idAuthor)
Author:  (name, id)

注意:每种技术都可以很容易地扩展到 2 位以上的作者。第二个查询甚至可以适应“这 5 位作者中的至少 3 位”:IN 中的 5 个姓名和 HAVING COUNT(*) >= 3

关于java - 使用 java 循环运行 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40521508/

相关文章:

java - 如何在同一测试中使用 2 个不同的参数捕获器类

java - 根据 ID 在 spritesheet 中显示纹理

java - 如何声明字段级类型参数?

java - java中有BlockingMap和BlockingQueue吗?

mysqldbcompare 访问被拒绝但 mysql 命令有效

mysql - 无法从 Left JoinWhere 和 group by 中找到预期结果

mysql - 如何在 MYSQL/SQL 中获取随机 10% 的数据

java - 车牌号验证程序JAVA

php - for & while循环条件比较

使用模板内的 getter 触发的 Angular 变化检测循环