MySQL Forums
Forum List  »  Performance

index not being pick up
Posted by: r r
Date: January 15, 2018 11:57PM

I have a situation where mysql is not using an index when the function "|| scopeOverrideEnabled()" is used in the view, see below . Remove
|| `scopeOverrideEnabled(). The uq_reference_idx will be used.


create or replace algorithm=merge view `vw_property`
as select *
from `property`
where (scope_id = getScopeId() || scopeOverrideEnabled())
with check option;

create table `property` (
`property_id` int(10) unsigned not null auto_increment,
`reference` varchar(15) not null,
`scope_id` int(10) unsigned,
`created_utc` datetime(3) not null default now(3),
`updated_utc` datetime(3) not null default now(3) on update now(3),
primary key (`property_id`),
unique key `uq_reference_idx` (`scope_id`, `reference`)
);

CREATE FUNCTION `getScopeId`() RETURNS int(10) unsigned DETERMINISTIC NO SQL
begin
return @current_scope_id;
end

create function `scopeOverrideEnabled` () returns bit no sql
begin
return @scope_override_enabled;
end

for this query I expect mysql to use uq_reference_idx key, but it doesn't. Is there anyway I could change the view to make mysql use the index?

explain select * from vw_property where reference = "200ADELAIDE";
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE property ALL uq_reference_idx NULL NULL NULL 151 Using where

I tried the following without success.
- Change the scopeOverrideEnabled() to be DETERMINISTIC
- Change the scopeOverrideEnabled() to return boolean
- In where change to scopeOverrideEnabled() = 1
- Set specific index for the reference works but not ideal, as we may need to end up creating multiple indexes for addition columns that are added. We would prefer to have the uq_reference_idx working.

For background info we are using the scope_id for multitenancy. Every property will have a scope_id and we set the scope_id when we access the db connection. So I would expect to be able to have the uq_reference_idx used as a fallback index.

Options: ReplyQuote


Subject
Views
Written By
Posted
index not being pick up
1364
r r
January 15, 2018 11:57PM
581
January 16, 2018 12:19AM
524
January 16, 2018 04:39PM
555
January 16, 2018 07:49PM
571
January 17, 2018 12:26AM
545
January 17, 2018 01:07AM
771
January 17, 2018 01:19AM
573
January 17, 2018 12:41PM
546
January 19, 2018 07:26AM
553
January 21, 2018 04:31PM
510
January 21, 2018 05:41PM
599
January 22, 2018 03:00AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.