In general, MarkLogic supports the syntax from the SQL92 standard. This chapter describes some of the SQL syntax that are unique to MarkLogic Server.
This section describes the SQL statements and functions supported in MarkLogic. The topics are:
MarkLogic SQL does not support updates, so only the SQL statements in the following table are supported.
SQL Statement | Notes |
---|---|
EXPLAIN | Produces an execution plan, as described in Execution Plan. |
SELECT | The following SELECT options are not supported: FULL OUTER JOIN, BLOB types, and correlated subqueries containing a GROUP BY. |
CREATE VIEW | Creates a view. Views created by this statement only exist for the duration of the SQL connection. |
DROP VIEW | Drops a view created by CREATE VIEW. |
MarkLogic supports the SQL functions in the SQL92 standard. In addition, MarkLogic supports SQL functions that are not part of the SQL92 standard, as shown in the table below. The SQL functions are listed along with the MarkLogic builtin functions that support them. The syntax for the SQL function is the same as that of the respective builtin function.
You can also call any MarkLogic builtin function in a SQL query, as described in Calling Built-in Functions from SQL.
The table below lists all of the supported SQL types in MarkLogic, along with the mapping from the SQL types to XML Schema (or MarkLogic) types. MarkLogic also supports a number of SQL type that go beyond those supported by the SQL92 standard, as well as some vendor specific types.
Limits on datatypes are not enforced. For example, if you enter DECIMAL(p,s)
, the precision and scale are ignored.
Data dictionaries consists of a series of tables that are created in the SYS schema. These system tables are listed in the table below.
To see the full contents of a system table, do a select *
. For example:
select * from sys_tables
Each view has two system columns:
Column Name | Description |
---|---|
__docid |
Identifies the fragment ID of each document that matches the view(s). |
__content |
Returns the content of document that matches the view(s). |
The __docid
and __content
system columns are preceeded by two underscores.
For example: The following returns the fragment ID for each document that matches the employees
view:
select __docid from employees
The following returns the contents of each document that matches the employees
view:
select __content from employees
You can call MarkLogic built-in functions from inside a SELECT statement, as long as the parameter types match the column types. You cannot call aggregate functions from SQL.
The following are some examples of the use of MarkLogic functions in SQl statements:
Provide the version of MarkLogic Server and hardware information:
select xdmp_version(), xdmp_platform(), xdmp_architecture()
Trace the performance of a query:
select xdmp_elapsed_time, t1.this, t2.that from t1, t2 where t1.key=t2.ref group by t1.this
select math_cos(EmployeeID) from employees
select cts_distance(town.center, building.location) from town, building
Return the first five values of the FirstName column, starting with the third character:
select fn_substring(FirstName,3) from employees limit 5
MarkLogic supports the ORDER BY keyword supported in SQL standard 2003, rather than sql92.
When you include an ORDER BY in SQL queries, such as ORDER BY column_A desc nulls first
, you are also able to specify where to put the nulls, either at the beginning (NULLS FIRST
) or at the bottom (NULLS LAST
). If you don't specify the nulls ordering, the default behavior is NULLS LAST
, putting all the nulls at the bottom, which is often the most efficient option.
If you want the default behavior to be NULLS FIRST
, enable the trace event:
Optic Nulls Smallest On
The MATCH operates differently on range views and template views. You can MATCH column names when using range views, but not template views. You can MATCH on tables created by both range and template views.
When the MATCH operator is used with range views, column names are bound to their corresponding index references and searchable fields are bound to their field names. When the MATCH operator is applied to individual columns, all names are unbound, as it doesn't make sense to constrain searches against one index to the values of another. These queries are executed in unfiltered mode.
The search expression following the MATCH operator must be contained inside single quotes.
Field names, like view and schema names, are treated as case-insensitive for the purposes of duplicate detection and lookup.
The following table lists the search grammar that can be used by the MATCH operator.
Type | Token |
---|---|
Wildcards* | ? % * |
Boolean Operators | AND, OR, NOT, NOT_IN, NEAR/integer |
Comparison Operators | EQ, NE, LT, LE, GT, GE |
Name Binding** | <field_name>:<value>, <column_name>:<value> |
* To use wildcards in a search expression, you must enable trailing wildcard searches
and word lexicons
(codepoint collation
) on your database.
** Searches are constrained to the named field or column values. The field or column text must have the correct case. For example, 'Position:Manager'
is not the same as 'position:Manager'
. Because you cannot specify fields in a template view, you cannot MATCH on field names.
The following queries will work on both range views and template views:
SELECT * FROM employees WHERE employees MATCH 'Manager' SELECT * FROM employees WHERE employees MATCH 'J*' SELECT employeeid, firstname, lastname, position FROM employees WHERE employees MATCH 'Steve OR John OR Goodall' SELECT employeeid, firstname, lastname, position FROM employees WHERE employees MATCH 'Steve AND Manager' SELECT * from employees WHERE firstname MATCH 'John OR Jane' AND lastname MATCH 'Lead'
The following queries will work on range views only:
SELECT * FROM employees WHERE employees MATCH 'position:Manager' SELECT firstname, lastname FROM employees WHERE employees MATCH 'employeeid LE 3' SELECT employeeid, firstname, lastname, position FROM employees WHERE firstname MATCH 'Steve OR John OR Goodall' SELECT * FROM employees WHERE employees MATCH 'firstname:J*'
The MarkLogic ODBC driver supports Postgres SET and SHOW run-time configuration parameters, as well as some parameters that are specific to MarkLogic Server. These parameters only work when accessing MarkLogic through an ODBC driver, as is the case with mlsql
. They do not work when accessing MarkLogic through xdmp:sql or the Query Console.
For details on the Postgres parameters, see:
All SET parameters are good for the duration of the SQL session in which they are set. Some parameters are read-only and can only be specified by the SHOW statement. These are described in Read-only SHOW Parameters.
All SET string values must be specified in single quotes (SET
parameter '
value'
).
Sets the timezone offset to that for the given timezone name. The standard permitted formats and keywords can be used.
For example, to set the timezone to UTC, enter:
SET timezone 'UTC'
Sets the timeout for statement execution (milliseconds).
SET statement_timeout 5000
Sets the default collation in the dynamic environment.
The form we will see from the Postgres client is:
SET lc_collate 'en_US.utf8'
This maps to the collation: http://marklogic.com/collation/en_US
You can also specify a full collation string:
SET lc_collation 'http://marklogic.com/collation/en_US/S1/MO'
Sets the number of digits displayed for floating point types.
SET extra_float_digits 2
Declares the encoding of data coming from the client.
SET client_encoding 'UTF8'
SET NAMES is the standard syntax for the same thing.
SET NAMES 'UTF8'
Set the default coordinate system for geospatial operations.
SET coordinate_system 'wgs84/double'
For more details, see The Governing Coordinate System and Controlling Coordinate System and Precision in the Search Developer's Guide.
Sets the default schema referenced by names in SQL statements.
SET search_path 'main'
Enable or disable the redundant check on normal (on full-text) query constraints on rows. Value is 1 (enable) or 0 (disable). The default is 0.
SET mls_redundant_check 1; SELECT title, year FROM songs WHERE year=1991;
The following parameters can be obtained via the SHOW statement but they are read-only and cannot be set via the SET statement.
MarkLogic SQL does not have a default/implicit limit for the rows returned. Queries that return large result sets, such as tens of thousands of rows, may perform poorly. Should you experience performance problems it is a best practice to page the results using the LIMIT
statement.