我有一个经典的ASP页面,其中我拥有所有客户端(大约4,000个),是否有效,我需要在4个不同的表中获取有关它们的信息。我目前正在这样做的方式,我的页面需要3,4,5分钟,具体取决于服务器的繁忙程度。有时我甚至在asp中收到消息说:“超出了脚本执行的最长时间”。
我的查询如下,
我得到了客户的个人信息
我得到了客户的销售
3.获得该信息后,我会对我的客户端数组执行for循环以获取其他信息。
在那个循环中,我做了5个不同的查询。
我得到的所有信息都在我的页面中 什么是最好的方法来做这一切?
查询(在法国): 1。
SELECT c.NoClient, c.NomClient, c.CodeClient, c.NoClientFront,c.NoVendeur,
c.VilleClient,c.DateOuverture",Telemarketing,Autre1 FROM Client c WHERE (left(c.CodeClient,1) = 'C' Or left(c.CodeClient,1) = 'N') ORDER BY c.NoClient
CREATE TABLE `client` (
`NoClient` decimal(7,0) NOT NULL DEFAULT '0',
`NoVendeur` decimal(3,0) NOT NULL DEFAULT '0',
`NomClient` varchar(35) NOT NULL DEFAULT '',
`AdresseClient` varchar(35) NOT NULL DEFAULT '',
`VilleClient` varchar(30) NOT NULL DEFAULT '',
`TelClient` varchar(10) NOT NULL DEFAULT '',
`CodeClient` char(3) NOT NULL DEFAULT '',
`PointPower` int(11) NOT NULL DEFAULT '0',
`PrevisionYonka` int(11) NOT NULL DEFAULT '0',
`PrevisionCollin` int(11) NOT NULL DEFAULT '0',
`TerritoireClient` int(3) NOT NULL DEFAULT '0',
`NoClientFront` int(7) NOT NULL DEFAULT '0',
`PersonneContact` varchar(20) NOT NULL DEFAULT '',
`CodeCredit` varchar(30) NOT NULL DEFAULT '',
`CodePostalClient` varchar(10) NOT NULL DEFAULT '',
`DateOuverture` date NOT NULL DEFAULT '0000-00-00',
`Master` int(7) NOT NULL DEFAULT '0',
`Telemarketing` varchar(20) NOT NULL DEFAULT '',
`GammeAutorisee1` char(2) NOT NULL DEFAULT '',
`GammeAutorisee2` char(2) NOT NULL DEFAULT '',
`GammeAutorisee3` char(2) NOT NULL DEFAULT '',
`GammeAutorisee4` char(2) NOT NULL DEFAULT '',
`GammeAutorisee5` char(2) NOT NULL DEFAULT '',
`MembrePower` char(1) NOT NULL DEFAULT '',
`NoteLivreur1` varchar(35) NOT NULL DEFAULT '',
`NoteLivreur2` varchar(35) NOT NULL DEFAULT '',
`PlanAVenirYonka` int(11) NOT NULL DEFAULT '0',
`PlanAVenirCollin` int(11) NOT NULL DEFAULT '0',
`Autre1` varchar(50) NOT NULL COMMENT 'Langue du client',
`Autre2` varchar(50) NOT NULL DEFAULT '',
`NoteClient` text NOT NULL,
`CompteInternet` int(1) unsigned NOT NULL,
`DEI` varchar(10) NOT NULL,
`Empty1` varchar(45) NOT NULL,
`Empty2` varchar(45) NOT NULL,
PRIMARY KEY (`NoClient`),
KEY `CodeClient` (`CodeClient`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
2
Select a.NoClient,a.Gamme,a.Vente,a.Annee,a.Mois,c.Dateouverture" from AchatClient a, Client c where c.NoClient = a.NoClient And a.Annee <= " & AnneeRanking
And a.Annee >= " & AnneeRanking - 1 order by a.NoClient
CREATE TABLE `achatclient` (
`NoClient` int(10) DEFAULT NULL,
`NoVendeur` int(3) DEFAULT NULL,
`Gamme` char(1) DEFAULT NULL,
`G1` char(1) DEFAULT NULL,
`G2` char(1) DEFAULT NULL,
`Annee` int(4) DEFAULT NULL,
`Mois` int(2) DEFAULT NULL,
`Vente` float(10,2) DEFAULT NULL,
KEY `NoClient` (`NoClient`,`NoVendeur`),
KEY `Vente` (`NoClient`,`Annee`,`Mois`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
我的FOR循环中的
一个)
Select DatePlanification from planification where NoClient=" & client(i)
And DatePlanification <= '" & todayDate & "' And Etat=0 Order by DatePlanification DESC limit 1
CREATE TABLE `planification` (
`NoPlanification` decimal(10,0) NOT NULL DEFAULT '0',
`IDUsager` varchar(30) DEFAULT NULL,
`NoClient` decimal(7,0) DEFAULT NULL,
`DatePlanification` date DEFAULT NULL,
`HeurePlanDebut` varchar(5) NOT NULL DEFAULT '',
`HeurePlanFin` varchar(5) NOT NULL DEFAULT '',
`LieuxPlanification` varchar(50) NOT NULL DEFAULT '',
`TypePlanification` varchar(50) NOT NULL DEFAULT '',
`NomPlanification` varchar(250) NOT NULL DEFAULT '',
`Commentaire` text NOT NULL,
`Confirmation` int(1) unsigned zerofill NOT NULL DEFAULT '0',
`TypeContact` varchar(50) NOT NULL DEFAULT '',
`GammeContact` varchar(50) NOT NULL DEFAULT '',
`DetailPersonnel` text NOT NULL,
`DetailPublic` text NOT NULL,
`DateEntree` date DEFAULT NULL,
`HeureEntree` time DEFAULT NULL,
`DateModifie` date DEFAULT NULL,
`HeureModifie` time DEFAULT NULL,
`Autre1` varchar(50) NOT NULL DEFAULT '',
`Autre2` varchar(50) NOT NULL DEFAULT '',
`Etat` int(1) NOT NULL DEFAULT '0',
`DateCheckIn` varchar(15) DEFAULT NULL,
`TimeCheckIn` varchar(15) DEFAULT NULL,
`PositionCheckIn` varchar(100) DEFAULT NULL,
`DateCheckOut` varchar(15) DEFAULT NULL,
`TimeCheckOut` varchar(15) DEFAULT NULL,
`PositionCheckOut` varchar(100) DEFAULT NULL,
`Empty1` varchar(45) DEFAULT NULL,
`Empty2` varchar(45) DEFAULT NULL,
PRIMARY KEY (`NoPlanification`),
KEY `IDUsager` (`IDUsager`,`DatePlanification`),
KEY `DatePlanification` (`DatePlanification`),
KEY `NoClient` (`NoClient`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
b)中
Select DateVisite from visite_kterm where NoClient = " & Client(i)
order by DateVisite DESC limit 1
CREATE TABLE `visite_kterm` (
`NoClient` int(7) DEFAULT NULL,
`DateVisite` date DEFAULT NULL,
`SequenceVisite` char(2) DEFAULT NULL,
`Usager` varchar(20) DEFAULT NULL,
`Description_1` varchar(72) DEFAULT NULL,
`Description_2` varchar(72) DEFAULT NULL,
`Description_3` varchar(72) DEFAULT NULL,
`Description_4` varchar(72) DEFAULT NULL,
`Description_5` varchar(72) DEFAULT NULL,
`Description_6` varchar(72) DEFAULT NULL,
`Description_7` varchar(72) DEFAULT NULL,
KEY `NoClient` (`NoClient`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
C)
Select DatePlanification from Planification where NoClient=" & Client(i)
And DatePlanification > '" & answerFromA & "' And Etat=0
Order by DatePlanification ASC limit 1;
d)SELECT DateFacture FROM listefactures where NoClient=" & client(i)
CREATE TABLE `listefactures` (
`NoFacture` varchar(7) NOT NULL DEFAULT '',
`NoPrise` int(6) DEFAULT NULL,
`NoClient` int(10) DEFAULT NULL,
`PrisePar` varchar(10) DEFAULT NULL,
`DateFacture` date DEFAULT NULL,
`AdresseEnvoie` text,
`Reference` varchar(10) DEFAULT NULL,
`SousTotal` float(10,2) DEFAULT NULL,
`Total` float(10,2) DEFAULT NULL,
`Terme` varchar(20) DEFAULT NULL,
`TaxePro` float(10,2) DEFAULT NULL,
`TaxeFed` float(10,2) DEFAULT NULL,
`Transporteur` varchar(20) DEFAULT NULL,
`FraisTransport` float(10,2) DEFAULT NULL,
`FraisManutention` float(10,2) DEFAULT NULL,
`Ouverture` char(1) DEFAULT NULL,
`FacturePower` char(1) DEFAULT NULL,
`CodeCredit` int(3) unsigned NOT NULL,
`RepOuverture` varchar(5) NOT NULL,
PRIMARY KEY (`NoFacture`),
KEY `NoClient` (`NoClient`,`Ouverture`),
KEY `NoPrise` (`NoPrise`),
KEY `DateFacture` (`DateFacture`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
E)
Select Email from SDS where NoCompte = " & Client(i)
And Email like '%@%' And Email like '%.%';
CREATE TABLE `sds` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`NoCompte` int(11) NOT NULL DEFAULT '0',
`Email` varchar(100) DEFAULT NULL,
`SiteWeb` varchar(255) DEFAULT NULL,
`NomProprietaire` varchar(100) DEFAULT NULL,
`ageproprietaire` varchar(20) DEFAULT NULL,
`StatutProprietaire` int(11) DEFAULT NULL,
`NomProprietaireDuLocataire` text,
`Localisation` int(11) DEFAULT NULL,
`Situation` varchar(30) NOT NULL DEFAULT '',
`ProprioEstEstheticienne` int(11) DEFAULT NULL,
`Travailencabine` int(11) NOT NULL DEFAULT '0',
`ProprioDepuis` varchar(5) NOT NULL DEFAULT '',
`NomPersonneContact` varchar(100) DEFAULT NULL,
`FonctionPersonneContact` int(11) DEFAULT NULL,
`NbEstheticienne` int(11) DEFAULT NULL,
`NbEmploye` int(11) DEFAULT NULL,
`NbCabineFacial` int(11) DEFAULT NULL,
`NbCabineCorporel` int(11) DEFAULT NULL,
`NbCabineAutre` int(11) DEFAULT NULL,
`NbCabineDouche` int(11) DEFAULT NULL,
`NbCabineBain` int(11) NOT NULL DEFAULT '0',
`NbCabineLitSolaire` int(11) NOT NULL DEFAULT '0',
`SoinsOfferts` int(11) DEFAULT NULL,
`Competition1` varchar(30) DEFAULT NULL,
`Competition2` varchar(30) DEFAULT NULL,
`Competition3` varchar(30) DEFAULT NULL,
`Competition4` varchar(30) DEFAULT NULL,
`Competition5` varchar(30) DEFAULT NULL,
`PotentielClient` varchar(15) DEFAULT NULL,
`EtatVitrine` int(11) DEFAULT NULL,
`EtatComptoireVitre` int(11) DEFAULT NULL,
`EtatEtalageMur` int(11) DEFAULT NULL,
`SurfaceIncitante` int(11) DEFAULT NULL,
`PopulationVille` int(11) DEFAULT NULL,
`NbCliniqueExistante` int(11) DEFAULT NULL,
`NbCliniqueNotre` int(11) DEFAULT NULL,
`ParticulariteVille` text NOT NULL,
`DateMAJ` date DEFAULT NULL,
`Email1` varchar(100) NOT NULL DEFAULT '',
`Contact1` varchar(50) NOT NULL DEFAULT '',
`titre1` varchar(50) NOT NULL DEFAULT '',
`Email2` varchar(100) NOT NULL DEFAULT '',
`Contact2` varchar(50) NOT NULL DEFAULT '',
`titre2` varchar(50) NOT NULL DEFAULT '',
`Email3` varchar(100) NOT NULL DEFAULT '',
`Contact3` varchar(50) NOT NULL DEFAULT '',
`titre3` varchar(50) NOT NULL DEFAULT '',
`Email4` varchar(100) NOT NULL DEFAULT '',
`Contact4` varchar(50) NOT NULL DEFAULT '',
`titre4` varchar(50) NOT NULL DEFAULT '',
`statutProspect` int(3) unsigned NOT NULL,
`dei` int(4) unsigned NOT NULL DEFAULT '0',
`deivalue` float NOT NULL DEFAULT '0',
PRIMARY KEY (`ID`,`NoCompte`)
) ENGINE=MyISAM AUTO_INCREMENT=7356 DEFAULT CHARSET=latin1
答案 0 :(得分:2)
我将所有查询合并为一个。我做了一些假设,比如每个客户只需要一条记录。需要添加的变量是花括号。
SELECT NoClient, Gamme, Vente, Annee, Mois, Dateouverture, NomClient, CodeClient,
NoClientFront, NoVendeur, VilleClient, DateOuverture, Telemarketing, Autre1,
MAX(DatePlanification) AS DatePlanification, MAX(DateVisite ) AS DateVisite, Email
FROM Client
JOIN AchatClient USING (NoClient)
JOIN planification USING (NoClient)
JOIN visite_kterm USING (NoClient)
JOIN DateFacture USING (NoClient)
LEFT JOIN SDS ON (NoClient = NoCompte)
WHERE (CodeClient LIKE 'C%' OR CodeClient LIKE 'N%')
AND Annee BETWEEN {AnneeRanking} AND {AnneeRanking - 1}
AND DatePlanification <= {todayDate}
AND Etat=0
AND Email like '%@%' AND Email like '%.%'
GROUP BY NoClient
ORDER BY NoClient
查询将受益于下面列出的索引:
SDS - single index on `NoCompte`
planification - make the `NoClient` index composite like (`NoClient`, `DatePlanification`)
achatclient - single index on `Annee`
此外,将所有NoClient
列设为INT
类型之一。
使用像这样的单个查询应该比大量的串行查询快多。您可能需要根据数据将部分JOIN
切换为LEFT JOIN
。