Mysql t对应于任何应用程序,并由用户从外部服务的“通用”版本创建或编辑
现在,我必须从表app_ws_user检索所有与应用程序相关的服务,并从表app_ws_common检索所有与应用程序相关的服务,而这些服务在app_ws_user表中没有该特定用户的编辑版本Mysql t对应于任何应用程序,并由用户从外部服务的“通用”版本创建或编辑,mysql,performance,database-design,Mysql,Performance,Database Design,现在,我必须从表app_ws_user检索所有与应用程序相关的服务,并从表app_ws_common检索所有与应用程序相关的服务,而这些服务在app_ws_user表中没有该特定用户的编辑版本 如何高效地为此编写查询 服务表DDL不清楚。请详细说明您的要求。我已经添加了一些详细信息。。希望这有助于更好地理解……)服务表DDL不清楚。请详细说明您的要求。我已经添加了一些详细信息。。希望这有助于更好地理解……)@Tom感谢您的输入:)该版本用于服务。因此,我可以创建一个“ServiceVersion
如何高效地为此编写查询 服务表DDL不清楚。请详细说明您的要求。我已经添加了一些详细信息。。希望这有助于更好地理解……)服务表DDL不清楚。请详细说明您的要求。我已经添加了一些详细信息。。希望这有助于更好地理解……)@Tom感谢您的输入:)该版本用于服务。因此,我可以创建一个“ServiceVersion[serviceid,userid,version]”表,该表将跟踪特定用户的服务版本。如果该表没有特定用户的任何条目,我可以获取“common”userid的记录,如果存在两条记录,一条记录为“common”,另一条记录为“a”。我将分析其工作原理并简化我的查询。@Tom感谢您的输入:)该版本用于服务。因此,我可以创建一个“ServiceVersion[serviceid,userid,version]”表,该表将跟踪特定用户的服务版本。如果该表没有特定用户的任何条目,我可以获取“common”userid的记录,如果存在两条记录,一条记录为“common”,另一条记录为“a”。我将分析这是如何工作的,并简化我的查询。
applicationid(pk)
userid(pk)
applicationname
serviceid(pk)
userid(pk)
applicationid
servicename
servicetype
version
modifieddate
select ser.*,app.applicationname
from services ser
left join applications app
on ser.userid = app.userid and ser.applicationid = app.applicationid
where (ser.userid = 'user1' OR ser.userid = 'common')
AND (ser.applicationid = 'appid1' OR ser.applicationid IS NULL)
AND (ser.modifieddate < '9999-01-01 00:00:00' OR (ser.modifieddate = '9999-01-01 00:00:00' AND ser.serviceid > ' \n'))
AND ser.version = (select max(ser1.version)
from services ser1
where (ser1.userid = 'user1' OR ser1.userid = 'common')
AND (ser1.applicationid = 'appid1' OR ser1.applicationid IS NULL)
AND ser1.servicename = ser.servicename)
ORDER BY ser.modifieddate,ser.serviceid
LIMIT 0,50
create table user
(id int unsigned not null primary key auto_increment,
userName varchar(50) not null,
unique key userUIdx (userName));
create table application
(id int unsigned not null primary key auto_increment,
applicationName varchar(50) not null,
unique key applicationUIdx1 (applicationName));
create table applicationVersion
(id int unsigned not null primary key auto_increment,
applicationId int unsigned not null,
version decimal(10,2) unsigned not null default 1.0,
userId int unsigned not null,
modifiedDate timestamp,
unique key applicationVersionUIdx1 (applicationId,version),
constraint `fk_applicationVersion_user1` FOREIGN KEY (`userId`) REFERENCES `user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
constraint `fk_applicationVersion_application1` FOREIGN KEY (`applicationId`) REFERENCES `application` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
);
create table service
(id int unsigned not null primary key auto_increment,
serviceName varchar(50) not null,
serviceType varchar(50) not null,
unique key serviceUIdx1 (serviceName));
create table serviceApplicationVersion
(id int unsigned not null primary key auto_increment,
applicationVersionId int unsigned not null,
serviceId int unsigned not null,
modifiedDate timestamp,
unique key serviceApplicationVersionUIdx1 (applicationVersionId,serviceId),
constraint `fk_serviceApplicationVersion_applicationVersion1` FOREIGN KEY (`applicationVersionId`) REFERENCES `applicationVersion` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
constraint `fk_serviceApplicationVersion_service1` FOREIGN KEY (`serviceId`) REFERENCES `service` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
);
insert into user (userName) values ('common');
insert into user (userName) values ('User A');
insert into user (userName) values ('User B');
insert into application (applicationName) values ('App 1');
insert into application (applicationName) values ('App 2');
insert into applicationVersion (applicationId,version,userId) values (
(select id from application where applicationName = 'App 1'),
1.0,
(select id from user where userName = 'common')
);
insert into applicationVersion (applicationId,version,userId) values (
(select id from application where applicationName = 'App 1'),
2.0,
(select id from user where userName = 'User A')
);
insert into service (serviceName, serviceType) values ('Service 1','Service with apps');
insert into service (serviceName, serviceType) values ('Service 2','Service with no apps');
insert into serviceApplicationVersion (applicationVersionId,serviceId) values (
(select av.id from applicationVersion av inner join application a on a.id = av.applicationId where a.applicationName = 'App 1' and av.version = 2.0),
(select id from service where serviceName = 'Service 1')
);
-- Query to select services,applications and versions in those services and users
select s.serviceName,
a.applicationName,
av.version,
u.userName
from user u
inner join applicationVersion av on av.userId = u.id
inner join serviceApplicationVersion sav on sav.applicationVersionId = av.id
inner join application a on av.applicationId = a.id
inner join service s on s.id = sav.serviceId
where userName = 'User A';
CREATE TABLE `app_ws_common` (
`serviceid` varchar(16) NOT NULL,
`applicationid` varchar(16) default NULL,
`modifieddate` date default NULL,
`version` decimal(1,0) default NULL,
`servicename` varchar(20) default NULL,
`userid` varchar(128) NOT NULL,
PRIMARY KEY (`serviceid`,`userid`),
KEY `table1_isv_fk` (`applicationid`,`userid`),
CONSTRAINT `table1_isv_fk` FOREIGN KEY (`applicationid`, `userid`) REFERENCES `applications` (`applicationid`, `userid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE TABLE `app_ws_user` (
`serviceid` varchar(16) NOT NULL,
`userid` varchar(128) NOT NULL,
`applicationid` varchar(16) default NULL,
`modifieddate` date default NULL,
`version` decimal(1,0) default NULL,
`servicename` varchar(20) default NULL,
PRIMARY KEY (`serviceid`,`userid`),
KEY `FK_app_ws_user` (`applicationid`,`userid`),
CONSTRAINT `FK_app_ws_user` FOREIGN KEY (`applicationid`, `userid`) REFERENCES `applications` (`applicationid`, `userid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE TABLE `applications` (
`applicationid` varchar(16) NOT NULL,
`userid` varchar(128) NOT NULL,
`applicationname` varchar(30) default NULL,
PRIMARY KEY (`applicationid`,`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE TABLE `external_ws_common` (
`serviceid` varchar(16) NOT NULL,
`modifieddate` date default NULL,
`version` decimal(1,0) default NULL,
`servicename` varchar(20) default NULL,
PRIMARY KEY (`serviceid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE TABLE `external_ws_user` (
`serviceid` varchar(16) NOT NULL,
`userid` varchar(128) NOT NULL,
`applicationid` varchar(16) default NULL,
`modifieddate` date default NULL,
`version` decimal(1,0) default NULL,
`servicename` varchar(20) default NULL,
PRIMARY KEY (`serviceid`,`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1