Debian Codegraph @

doc/source/reference/functions/in_values.rst

raw
1 .. -*- rst -*-
2
3 .. groonga-command
4 .. database: functions_in_values
5
6 ``in_values``
7 =============
8
9 Summary
10 -------
11
12 .. versionadded:: 4.0.7
13
14 ``in_values`` enables you to simplify the query which uses multiple ``OR`` or ``==``. It is recommended to use this function in point of view about performance improvements in such a case.
15
16 Syntax
17 ------
18
19 ``in_values`` requires two or more arguments - ``target_value``, multiple ``value``, and options.
20
21 ::
22
23 in_values(target_value, value1, ..., valueN)
24 in_values(target_value, value1, ..., valueN, {"option": "value of option"}
25
26 Usage
27 -----
28
29 Here is a schema definition and sample data.
30
31 Sample schema:
32
33 .. groonga-command
34 .. include:: ../../example/reference/functions/in_values/usage_setup_schema.log
35 .. table_create Tags TABLE_PAT_KEY ShortText
36 .. table_create Memos TABLE_HASH_KEY ShortText
37 .. column_create Memos tag COLUMN_SCALAR ShortText
38 .. column_create Tags memos_tag COLUMN_INDEX Memos tag
39
40 Sample data:
41
42 .. groonga-command
43 .. include:: ../../example/reference/functions/in_values/usage_setup_data.log
44 .. load --table Memos
45 .. [
46 .. {"_key": "Groonga is fast", "tag": "groonga"},
47 .. {"_key": "Mroonga is fast", "tag": "mroonga"},
48 .. {"_key": "Rroonga is fast", "tag": "rroonga"},
49 .. {"_key": "Droonga is fast", "tag": "droonga"},
50 .. {"_key": "Groonga is a HTTP server", "tag": "groonga"}
51 .. ]
52
53 Here is the simple usage of ``in_values`` function which selects the records - the value of ``tag`` column is "groonga" or "mroonga" or "droonga".
54
55 .. groonga-command
56 .. include:: ../../example/reference/functions/in_values/usage_only.log
57 .. select Memos --output_columns _key,tag --filter 'in_values(tag, "groonga", "mroonga", "droonga")' --sort_keys _id
58
59 When executing the above query, you can get the records except "rroonga" because "rroonga" is not specified as value in ``in_values``.
60
61 Then, you can also specify options of ``in_values``.
62 Currently, you can only specify ``too_many_index_match_ratio``. The type of this value is ``double``.
63
64 You can change the value of ``GRN_IN_VALUES_TOO_MANY_INDEX_MATCH_RATIO`` with ``too_many_index_match_ratio``.
65 The default value of ``GRN_IN_VALUES_TOO_MANY_INDEX_MATCH_RATIO`` is ``0.01``.
66 ``GRN_IN_VALUES_TOO_MANY_INDEX_MATCH_RATIO`` is used for deciding whether ``in_values`` use an index or not.
67
68 There is a case that sequential search is faster than index search when the number of narrowed down records is small enough in contrast to the number of expected records to narrow down by ``in_values`` with AND operation which use indexes.
69
70 For example, suppose you narrow down records by ``--filter`` and you narrow down them by ``in_values``.
71
72 In the default, ``in_values`` use sequential search in the following case.
73
74 1. If you narrow down records to 1,000 by ``--filter`` and records of the target of ``in_values`` are 500,000.
75
76 .. code-block::
77
78 1,000/500,000 = 0.002 < 0.01(GRN_IN_VALUES_TOO_MANY_INDEX_MATCH_RATIO) -> in_values use sequential search.
79
80 On the other hand, ``in_values`` use index in the following case.
81
82 2. If you narrow down records to 1,000 by ``--filter`` and records of the target of ``in_values`` are 50,000.
83
84 .. code-block::
85
86 1,000/50,000 = 0.02 > 0.01(GRN_IN_VALUES_TOO_MANY_INDEX_MATCH_RATIO) -> in_values use index.
87
88 Here is a query to set options of ``in_values``:
89
90 .. groonga-command
91 .. include:: ../../example/reference/functions/in_values/usage_options.log
92 .. select Memos --output_columns _key,tag --filter 'in_values(tag, "groonga", "mroonga", "droonga", {"too_many_index_match_ratio":0.001})' --sort_keys _id
93
94 Parameters
95 ----------
96
97 There are two or more required parameter, ``target_value`` and multiple ``value``.
98
99 ``target_value``
100 ^^^^^^^^^^^^^^^^
101
102 Specifies a column of the table that is specified by ``table`` parameter in ``select``.
103
104 ``value``
105 ^^^^^^^^^
106
107 Specifies a value of the column which you want to select.
108
109 ``{"option": "value of option"}``
110 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
111
112 Specify in_values's option.
113 Currently, you can only specify ``too_many_index_match_ratio``. The type of this value is ``double``.
114
115 Return value
116 ------------
117
118 ``in_values`` returns whether the value of column exists in specified the value of parameters or not.
119
120 If record is matched to specified the value of parameters, it returns true. Otherwise, it returns false.