Mysql query how diverse the same table / sql sub-table queries, the development of ideas java project log table partition table / table by month

Prior to the development of a monitoring system, database log table is a single table, although the data is not large and did a sql query optimization, but after the database log table data will certainly be increasingly large, it will cause the query is slow, so the log table to change the ingredient list, log table can be done according to the level of sub-table time, I was by month, and every month a table, this time the problem is

  1. How Zhang same database multiple sub-table query based on conditions?
  2. How to calculate the total time during the paging number of records? How to check out all the points table?
  3. The new table every month is how to create? How the system automatically creates?
  4. How sure which case a sub-table queries a detailed record?
    Here Insert Picture Description

Sub-table query
sub-table can be queried with a query union or union all
union and the two results are the union all combined set

select * from log_2019_05  where createTime > '2019-05-01 10:00:00'
union all
select * from log_2019_06  where createTime < '2019-06-01 10:00:00' 

In our java program which, according to the query start time and end time of the transfer of user, you can parse out those tables to query, and then spliced ​​into the sql we have to check on it, for example: check table can be placed inside the array, and then process the query in xml mybatis which loops through the splicing sql

NOTE:
Union to re-sort and
union all does not re-sort
the union all faster performance, less than sorting union, union is also sort data after the merge, remove duplicate in ordering
the log table is not repeated here of itself is the chronological order, so with the union all

Calculate the total number of records points table
if you want to place a page into the list of pages you want to display the total number of records, you can check out all the points table, and then after all stitching union with count (*), that problem again, how check out all of the same sub-table structure

  1. Idea 1: We are the points table by month, just to get the current month in the program, and our table was first created in the month, you can calculate all the points table ( the bottom there is all the months between the two dates java code )
  2. The idea II: monthly sub-table, we do not have to get all the points total number of records in the database table, you can give a start time defaults (can be 1 month default default three months ago, etc.) in the query page, on statistics During this time the points table
  3. The idea Three: If our table is not divided by month, then how do you know all of the points table it? This time you can can check out all of the points table records with tables information_schema database tables in mysql
select table_name from INFORMATION_SCHEMA.TABLES where table_name like 'log_%'

Here Insert Picture Description
Finally, I used program is the default search criteria page to query the current month, the first coming in the log list page to display paged data for the current month, so there is no need to query all of the points table (if the query when the start and end time of the election , the calculated between the beginning of the end of the month all the time, union all connected to the corresponding month of the query table)
Here Insert Picture Description

Note
information_schema.tables metadata stored information data table
information_schema database tables are read-only, can not be updated, deletes, and inserts the operation

Monthly sub-table, how every time you want to create

  1. Idea 1: The simplest is to manually create, create their own hand next two years
  2. The idea II: manually create a new table can not once and for all, certainly have the program automatically create a job, you can use regular tasks, executed once a month on the 1st, create sub-table next month, or once every period of time to perform, to create the next six months 2012 or table (if not created)
  3. The idea three: one can estimate the current month table exists, does not exist in the new program into a log is created when then insert data
  4. The idea four: three ideas but can simply create a table once a month, most do not need to judge each time to determine whether there is a bit redundant and loss performance, optimization on this basis, a principle similar optimistic locking, each when they are considered to insert data in the table already exists, direct insertion, an exception is thrown when the table does not exist, capturing the abnormal new table, and then insert the data

A detailed record of how to query a table of points
in a list of pages to see just outline log, as well as detailed logs need to click into the detailed page view, the question is, just before the id parameter passed to it, and now in the points table each table are likely to have the same id, so wearing an id certainly not at this time need more time to wear a parameter to create, in accordance with this program which was created to know goes on the score sheet, and then according to id query.

Note:
If the points table id uses the globally unique ID of the program, all of the points table id is not the same, but also can be used directly out of the query ID

All months between two dates

    /**
	 *
	 * @param minDate 最小时间  
	 * @param maxDate 最大时间
	 * @return 日期集合 格式为 年-月
	 * @throws Exception
	 */
	public static List<String> getMonthBetween(String minDate, String maxDate) throws Exception { ArrayList<String> result = new ArrayList<String>(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月 SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMM");//格式化为年月 Calendar min = Calendar.getInstance(); Calendar max = Calendar.getInstance(); min.setTime(sdf.parse(minDate)); min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1); max.setTime(sdf.parse(maxDate)); max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2); Calendar curr = min; while (curr.before(max)) { result.add(sdf2.format(curr.getTime())); curr.add(Calendar.MONTH, 1); } return result; } public static void main(String[] args) throws Exception{ String minDate = "2018-09-02 00:15:01"; String maxDate = "2019-06-02 00:15:01"; List<String> list = DateUtils.getMonthBetween(minDate,maxDate); for(String date :list){ System.out.println("时间:"+date); } } 

Run the main method results
Here Insert Picture Description

 

 

Original link: https: //www.csdn.net/gather_20/MtTaUg3sODk3OC1ibG9n.html

Guess you like

Origin www.cnblogs.com/ztf20/p/12304593.html