Successfully!

Error!

Database Query

  • Last update:  2023-06-08
  • Overview

    Version

    Version

    Functional Change

    11.0

    /

    11.0.5

    If you select Postgre database connection, you can only add tables under the mode that you have selected at the time of connection.

    11.0.16

    Allowing adding comments before SQL statements in database logs to help troubleshoot performance problems.

     Functions

    Database query refers to selecting the required fields through SQL statements from a defined database connection, namely the data source. With database query, you can manipulate the database tables in the data source directly.

    Query Methods

    There are three types of database query methods: table query, view query, and fuzzy query.

    Table Query

    Query Steps

    1. Click Template > Template Dataset to go to the setting page. Click 加号.png in the upper left corner of Template Dataset setting page. Select DB Query and name the query. Select the required data connection.

    图一.png

    2. Select the query editor and enter the SQL statement SELECT * FROM ORDERS where ORDERID=${ID}This is a SQL query with a parameter. ${} is the parameter format, and the ID in ${} is the name of the parameter. Click Preview, and a prompt will pop up. Enter the parameter ID=10004. You can see the result in the preview group, which is actually executing the SQL query SELECT * FROM ORDERS where ORDERID=10004.

    图三.png

    Note:

    1. Standard SQL statements are applicable. You just need to put parameters in ${}.

    2. If the parameter name is a combination of letters and numbers (such as A1), the system will consider it as a cell. If the default value is not set, it will trigger template calculation, which will cause one SQL execution. Therefore, it is recommended for you to avoid using a combination of letters and numbers as parameters. If you have to use this type of parameters, you need to set a default value for the parameter.

     Basic Properties of Dataset Interface

    Types

    For query types, FineReport supports both general SQL queries and stored procedures. Both types can contain parameters.

    Note: SQL queries and stored procedures are ultimately parsed and executed through JDBC by database software. The results are then returned to FineReport for display. Therefore, the speed and performance of the query are completely determined by the corresponding database software.

    Wizard

    FineReport provides a simple wizard to help you create simple SQL queries and stored procedures.

    Note: Due to the complexity of SQL statements, the wizard can only define simple SQL queries that are commonly used. It does not support grouping, sub-query and so on.

    The wizard for stored procedures can meet most of your requirements. It can list all the stored procedures in the connected database, and automatically obtain the parameters of the stored procedure to generate stored procedure statements that can be executed based on the name of the selected stored procedure.

    Query Editor

    When the query generated by the wizard cannot meet your requirements, you can use the text editor to edit any complex query statements. In most cases, you do not need to edit again when using the wizard to produce stored procedures.

    Parameter

    FineReport supports parameter queries. When you define   query statements, use the special string ${XXX}. If the parameter value is numeric, use ${parameter}, otherwise, use ‘${XXX}’ to enclose the parameter with single quotation marks. Click 刷新.png to assign default values to parameters. When the report engine executes this SQL statement, a prompt will pop up in the C/S system to ask you to enter the corresponding parameter value, and the default value will be automatically displayed. The B/S system will traverse   all the parameter values passed by client-end (usually the browser). FineReport uses these parameter values to replace the corresponding string   ${XXX} in queries, and submits the replaced query statements to the database for parsing and execution.

    Examples:

    1. The query statement is select * from Customers   where ID = ${ID}.

    2. The value of ID obtained dynamically from client-end is 1.

    3. Generate a new query statement based on the   parameter value: select * from Customers where ID = 1, and use JDBC to pass   this newly generated query statement to the corresponding database for   parsing and execution.

    Maximum Number of Preview Lines

    To improve the preview speed, the system sets   maximum number of preview lines (the default is 200). This value will only   take effect when you click Preview. It will not take effect on the   template files that references this query.

    View Query

    View query refers to the operations on the views defined in the data source.

    Database Query

    1. Click Template > Template Dataset to go to the setting page. Click 加号.png in the upper left corner of Template Dataset setting page. Select DB Query and name the query. Select the required data connection.

    图一.png

     Using View Query

    Select data source and enter the SQL statement for view query. For example:

    Enter the SQL statement SELECT * FROM [sys].[types] in the query editor. [sys].[types] is the name of a view query. Click Preview to see the reference to the view query.

    图三三.png

    Note: You can drag tables and views in data connection to query editor.

    Fuzzy Query

    There is a blank search box below the table, which is used for fuzzy queries. For example, if you want to search for tables related to client, enter client in the search box. Fuzzy matching will be performed automatically.

    查询标注.png

    Note:

    1. FineReport database query only supports one SQL statement such as SELECT * FROM A SELECT * FROM B. It does not support multiple SQL statements.

    2. Statements like CREATE and DROP are not officially supported. FineReport only supports SELECT statements.

     Copying SQL Statements

    In previous versions, it was inconvenient to directly copy SQL statements from the query interface to other interfaces if the statement contains parameters.

    FineReport 10.0.17 and later versions added View execution SQL function, which allows users to copy and paste SQL statements to other database software for troubleshooting and testing.

    Examples

    Click View execution SQL button at the top of the query interface. Because the SQL statement contains parameters, the parameter input interface will automatically pop up.

    13.png

    Executed SQL statements are automatically copied to the clipboard, and you can paste them to other database software for troubleshooting and testing.

     成功.png

    Notes

    1. If you call a custom function in SQL, View Executed SQL cannot run normally. The copied SQL statements cannot run properly in other database software.

    2. If you have enabled the escape function for anti-SQL injection, when a parameter matches an escape character, the query result will be affected. In this case, the system will prompt: Escape some parameters Prevent SQL injection, the query result may not match the expectation, please check and modify the parameters or contact the administrator.

    Hover your cursor over 注意.png in the upper right corner to view escaped parameters. In this example, the entered parameter value 1002 is the escaped parameter.

    防注入.png

     Troubleshooting Performance Problems

    FineReport 11.0.16 and later versions allow adding comments in database log before SQL statements to help troubleshoot performance problems.

    SQL Comments

    The SQL query scenarios and corresponding comments are shown in the following table:

    Note:

    1. Template name, dataset name and task name in the comments are encrypted with base64. You can use decoding tools to decode and view them.

    2. Does not support displaying SQL comments in stored procedures.

    SQL Query Scenario

    Log Format

    Effect

    Preview dataset in the designer

    --DesignerDatasetPreview_[dataset name]_[template   name being edited]

    SQL Statement

    1.png

    Preview server dataset on decision-making platform

    -- DatasetPreview_[dataset name]

    SQL Statement

    2.png

    Preview templates

    -- TemplatePreviewFetch_[template name]

    SQL Statement

    3.png

    Extracted data cache

    -- ESDFetch_[template name]

    SQL Statement

    4.png

    Task schedule

    -- ScheduleFetch_[template name]_[task name]

    SQL Statement

    5.png

    Database Log Query

    MySQL

    Query statements executed by MySQL server will be recorded in General Query Log. General Query Log is disabled by default.

    You can use the following commands to check whether General Query Log is enabled. The variable general_log_file defines the location of log files.

    mysql>SHOW VARIABLES LIKE '%general%';

    1.png

     If General Query Log is disabled, you can enable it by modifying the configuration file my.cnf of MySQL.

    general_log_file = /var/log/mysql/mysql.log    #general_log_file is the output directory of log.
    general_log = 1    #Setting general_log to 1 means enabling log query.

     After modification, save the configuration file and restart MySQL.

    Oracle

    v$sql records your SQL execution information in shared pools. You can query detailed SQL information and the time of execution through SQL statements.

    Note: If you cannot query the executed SQL statements, the executed SQL might have been replaced from the shared SQL area.

    SELECT SQL_TEXT,FIRST_LOAD_TIME
    FROM v$sql

    2.png

    SQL Server

    sys.dm_exec_query_stats records query statements in SQL Server cache. You can query detailed SQL information and the time of execution through SQL statements.

    Note: sys.dm_exec_query_stats only records query data that have been executed.

    SELECT TOP 1000
    --Creation time
    QS.creation_time,
    --Query statements
    SUBSTRING(ST.text,(QS.statement_start_offset/2)+1,
    ((CASE QS.statement_end_offset WHEN -1 THEN DATALENGTH(st.text)
    ELSE QS.statement_end_offset END - QS.statement_start_offset)/2) + 1
    ) AS statement_text,
    --Execution text
    ST.text,
    --Execution plans
    QS.total_worker_time,
    QS.last_worker_time,
    QS.max_worker_time,
    QS.min_worker_time
    FROM
    sys.dm_exec_query_stats QS
    --Keys
    CROSS APPLY
    sys.dm_exec_sql_text(QS.sql_handle) ST
    WHERE
    QS.creation_time BETWEEN '2011-10-20 16:00:00' AND '2023-10-20 17:00:00'
    AND ST.text LIKE '%%'
    ORDER BY
    QS.creation_time DESC

    333.png

    Attachment List


    Theme: Lauren待翻译
    • Helpful
    • Not helpful
    • Only read

    Doc Feedback