Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java MySQL-优化我的查询并返回更具体的匹配_Java_Mysql - Fatal编程技术网

Java MySQL-优化我的查询并返回更具体的匹配

Java MySQL-优化我的查询并返回更具体的匹配,java,mysql,Java,Mysql,不久前,我发布了一个问题,询问如何使用以下条件实施搜索: On the "Searching" page, the customer can search for movies by any of the following attributes or their combination (logical "AND" operation): title; year; director; star's first name and/or last name. This means you need

不久前,我发布了一个问题,询问如何使用以下条件实施搜索:

On the "Searching" page, the customer can search for movies by any of the following attributes or their combination (logical "AND" operation):
title;
year;
director;
star's first name and/or last name. This means you need to do both: a) first name or last name if only one of the two names is provided; b) first name and last name, if both names are provided.
然后,在给定搜索输入的情况下,我必须从我的数据库中吐出关于那部电影的信息。人们建议我使用全文搜索,我做到了。这就是我想出的解决办法

  String searchInput = request.getParameter("searchInput");
  String query = "";

  // First see if there is a number in their search
  // Note: doesn't consider more specific searches, because it only searches
  // based on the given number
  if (searchInput.matches("\\d+")) {
      Pattern p = Pattern.compile("\\d+");
      Matcher m = p.matcher(searchInput); 
      if (m.find()) {
          String number = m.group();
          int n = Integer.parseInt(number);
          query = "SELECT title, year, director, id " +
          "FROM movies " + 
          "WHERE year = " + n + " " +
          "OR title LIKE '%" + n + "%'";
      }
  } else {
      query = "SELECT title, year, director, r.id " +
      "FROM movies AS r, stars AS s " + 
      "WHERE MATCH(title, director) AGAINST('" + searchInput + "*') " + 
      "OR MATCH(first_name, last_name) AGAINST('" + searchInput + "*') " +
      "AND r.id IN (SELECT sim.movie_id " + 
                    "FROM stars_in_movies AS sim " + 
                    "WHERE sim.star_id = s.id " + 
                    "AND sim.movie_id = r.id) " +
      "GROUP BY title";
  }

  ResultSet rs = statement.executeQuery(query);

  // Query again, this time ANDing the matches to see if
  // there is a more specific search result
  Statement statement2 = dbcon.createStatement();
  query = "SELECT title, year, director, r.id " +
  "FROM movies AS r, stars AS s " + 
  "WHERE MATCH(title, director) AGAINST('" + searchInput + "*') " + 
  "AND MATCH(first_name, last_name) AGAINST('" + searchInput + "*') " +
  "AND r.id IN (SELECT sim.movie_id " + 
                "FROM stars_in_movies AS sim " + 
                "WHERE sim.star_id = s.id " + 
                "AND sim.movie_id = r.id) " +
  "GROUP BY title";

  ResultSet rs2 = statement2.executeQuery(query);
  if (rs2.next()) {
      // If there is a more specific match (ANDing the matches), use that instead
      rs2.beforeFirst();
      rs = rs2;
  }
现在,我知道这是非常糟糕的代码,因为我执行了两次几乎相同的查询,唯一的区别是我和第二个查询中的匹配项。我似乎无法找到一种方法来组合这两个查询,或者同时减少匹配量以加快查询。我也没有一个好方法来处理用户可能输入或不输入的电影年份

以下是我尝试过的:

  • 我尝试在全文搜索文档中搜索一些可以包含在搜索中的函数,以使我的SQL查询在匹配更多内容时返回更具体的结果,但唯一与此最接近的是布尔全文搜索。但是,我认为我不能使用这样的函数,因为我必须区分不同用户的关键字输入

  • 我还尝试过在MySQL中使用谷歌搜索和/或操作,允许查询,如果可能,则执行查询。然而,似乎不可能做到这一点


  • 更新:我现在有嵌套的子查询,搜索速度更快。但是,我仍然需要帮助改进代码,以返回更具体的匹配,并且仅返回更具体的匹配(如果存在)。否则,返回更一般的匹配项(如果所有匹配项都有任何匹配项)。

    分别运行每个子查询。假设结果的大小合理,您可以根据需要生成代码中的AND和OR。更多的编码,但是只运行一次必要的查询。

    为什么不使用存储过程来处理类似的事情?存储过程是什么意思?你是说准备好的声明?我认为我不能使用预先准备好的语句,因为我没有更改值。我正在更改“和”。不,存储过程它们比将所有SQL放在代码中要好得多。嗯,我正在想一种方法来分别运行子查询,但我不确定如何运行。你必须分别运行两个子查询。这只取决于您在何处实现AND/OR逻辑,可以在DB或代码中实现,只取决于哪个更适合您。