0
目前,我正在研究一個應用程序,該應用程序必須解析大型XML文件並使用其中的數據在數據庫中創建實體。用於創建實體的代碼是一個控制器調用的以下部分:減少創建時的數據庫查詢
def create
parser = EuroPassVacancy.new params[:vacancy][:file].tempfile.path
ids = parser.collect_vacancy_ids
ids.each do |vacancy_id|
country = Country.find_or_create_by_code(:code => parser.country_code(vacancy_id), :name => parser.country_code(vacancy_id))
company = Company.find_or_create_by_name(:name => parser.employer_name(vacancy_id), :address => parser.employer_address(vacancy_id),
:contact => parser.employer_contact(vacancy_id), :phone => parser.employer_phone(vacancy_id),
:email => parser.employer_email(vacancy_id), :fax => parser.employer_fax(vacancy_id),
:country => country)
Vacancy.create(:pes_id => parser.pes_id(vacancy_id), :title => parser.title(vacancy_id), :description => parser.description(vacancy_id),
:country => country, :region_code => parser.region_code(vacancy_id), :company_id => company, :isco_code => parser.isco_code(vacancy_id),
:nace_code => parser.nace_code(vacancy_id))
end
redirect_to :action => :index
end
這是一個相當沉重的循環,但我有理由相信,解析會確定。目前困擾我的是每個創建語句生成的查詢數量。以下是空缺的2 INSERT語句的小副本:
AREL (0.0ms) INSERT INTO `vacancies` (`country_id`, `pes_id`, `title`, `region_code`, `isco_code`, `created_at`, `updated_at`, `description`, `company_id`, `nace_code`) VALUES (9, 50, 'Konstruktér strojnÃ, StrojÃrenÅ¡tà technici projektanti, konstruktéři', 'RL041', 3118, '2011-05-25 13:19:16', '2011-05-25 13:19:16', 'ÚSO - strojnÃ. Konstrukce nástrojů na tvářenà a lisovánà kovů, forem na lisovánà plastů. Znalost tech. a právnÃch norem, ISO 9001, 14001 souvisejÃcÃch s výrobnÃm programem v rozsahu potÅ™ebném pro vývoj produktu a konstrukci výrobnÃch prostÅ™edků - lisovánà a tvářenà kovů, plastů, montážnÃch pÅ™Ãpravků, PC - konstrukÄ�nà SW - CAD, 3-D (Solid Edge výhodou), OFFICE Outlook, NJ/AJ uživatelská úroveň, ŘP sk. B.', 1, 0)
SQL (31.2ms) COMMIT
Country Load (0.0ms) SELECT `countries`.* FROM `countries` WHERE `countries`.`code` = 'cy' LIMIT 1
SQL (0.0ms) BEGIN
AREL (0.0ms) INSERT INTO `countries` (`updated_at`, `name`, `code`, `created_at`) VALUES ('2011-05-25 13:19:18', 'cy', 'cy', '2011-05-25 13:19:18')
SQL (63.0ms) COMMIT
Company Load (0.0ms) SELECT `companies`.* FROM `companies` WHERE `companies`.`name` = 'ΚΛΕΙΤΟΣ Î「ΕΩΡÎ「ΙΟΥ & ΥΙΟΣ ΛΙΜΙΤΕÎ」' LIMIT 1
SQL (0.0ms) BEGIN
SQL (0.0ms) SELECT 1 FROM `companies` WHERE `companies`.`name` = 'ΚΛΕΙΤΟΣ Î「ΕΩΡÎ「ΙΟΥ & ΥΙΟΣ ΛΙΜΙΤΕÎ」' AND `companies`.`country_id` = 10 AND (`companies`.`name` = BINARY 'ΚΛΕΙΤΟΣ Î「ΕΩΡÎ「ΙΟΥ & ΥΙΟΣ ΛΙΜΙΤΕÎ」') LIMIT 1
SQL (0.0ms) ROLLBACK
SQL (0.0ms) BEGIN
SQL (0.0ms) ROLLBACK
Country Load (0.0ms) SELECT `countries`.* FROM `countries` WHERE `countries`.`code` = 'cy' LIMIT 1
Company Load (0.0ms) SELECT `companies`.* FROM `companies` WHERE `companies`.`name` = 'ΠΑÎ�ΙΚΚΟΣ ΠΙΣΣΟΥΡΙΟΣ ΚΑΙ ΑÎ」ΕΛΦΟΣ ΚΟ ΛΤÎ」' LIMIT 1
SQL (0.0ms) BEGIN
SQL (15.6ms) SELECT 1 FROM `companies` WHERE `companies`.`name` = 'ΠΑÎ�ΙΚΚΟΣ ΠΙΣΣΟΥΡΙΟΣ ΚΑΙ ΑÎ」ΕΛΦΟΣ ΚΟ ΛΤÎ」' AND `companies`.`country_id` = 10 AND (`companies`.`name` = BINARY 'ΠΑÎ�ΙΚΚΟΣ ΠΙΣΣΟΥΡΙΟΣ ΚΑΙ ΑÎ」ΕΛΦΟΣ ΚΟ ΛΤÎ」') LIMIT 1
SQL (0.0ms) ROLLBACK
SQL (0.0ms) BEGIN
SQL (0.0ms) ROLLBACK
CACHE (0.0ms) SELECT `countries`.* FROM `countries` WHERE `countries`.`code` = 'cy' LIMIT 1
Company Load (0.0ms) SELECT `companies`.* FROM `companies` WHERE `companies`.`name` = 'THE DELHI PALACE INDIAN RESTAURANT LTD' LIMIT 1
SQL (0.0ms) BEGIN
SQL (0.0ms) SELECT 1 FROM `companies` WHERE `companies`.`name` = 'THE DELHI PALACE INDIAN RESTAURANT LTD' AND `companies`.`country_id` = 10 AND (`companies`.`name` = BINARY 'THE DELHI PALACE INDIAN RESTAURANT LTD') LIMIT 1
SQL (0.0ms) ROLLBACK
SQL (0.0ms) BEGIN
SQL (0.0ms) ROLLBACK
CACHE (0.0ms) SELECT `countries`.* FROM `countries` WHERE `countries`.`code` = 'cy' LIMIT 1
Company Load (0.0ms) SELECT `companies`.* FROM `companies` WHERE `companies`.`name` = 'EURES ADVISOR , STELLA PAPADOPOULOU APPIOU' LIMIT 1
SQL (0.0ms) BEGIN
SQL (0.0ms) SELECT 1 FROM `companies` WHERE `companies`.`name` = 'EURES ADVISOR , STELLA PAPADOPOULOU APPIOU' AND `companies`.`country_id` = 10 AND (`companies`.`name` = BINARY 'EURES ADVISOR , STELLA PAPADOPOULOU APPIOU') LIMIT 1
AREL (0.0ms) INSERT INTO `companies` (`email`, `phone`, `country_id`, `created_at`, `address`, `contact`, `fax`, `updated_at`, `name`) VALUES ('[email protected]', '00357 22 403000', 10, '2011-05-25 13:20:13', 'MOUSEIOU STREET 3 1102 , ΛΕΥΚΩΣΙΑ', 'STELLA PAPADOPOULOU APPIOU', '00357 22 873170/1', '2011-05-25 13:20:13', 'EURES ADVISOR , STELLA PAPADOPOULOU APPIOU')
SQL (31.2ms) COMMIT
SQL (0.0ms) BEGIN
SQL (0.0ms) ROLLBACK
Country Load (0.0ms) SELECT `countries`.* FROM `countries` WHERE `countries`.`code` = 'cy' LIMIT 1
Company Load (0.0ms) SELECT `companies`.* FROM `companies` WHERE `companies`.`name` = 'EURES ASSISTANT , NORA LANDA' LIMIT 1
SQL (0.0ms) BEGIN
SQL (0.0ms) SELECT 1 FROM `companies` WHERE `companies`.`name` = 'EURES ASSISTANT , NORA LANDA' AND `companies`.`country_id` = 10 AND (`companies`.`name` = BINARY 'EURES ASSISTANT , NORA LANDA') LIMIT 1
AREL (0.0ms) INSERT INTO `companies` (`email`, `phone`, `country_id`, `created_at`, `address`, `contact`, `fax`, `updated_at`, `name`) VALUES ('[email protected]', '00357 25 827327', 10, '2011-05-25 13:20:29', '67 FRANKLIN ROOSEVELT AV 3011 , ΛΕΜΕΣΟΣ', 'NOT AVAILABLE', '00357 25 306563', '2011-05-25 13:20:29', 'EURES ASSISTANT , NORA LANDA')
SQL (31.2ms) COMMIT
SQL (0.0ms) BEGIN
SQL (0.0ms) ROLLBACK
Country Load (0.0ms) SELECT `countries`.* FROM `countries` WHERE `countries`.`code` = 'cy' LIMIT 1
Company Load (0.0ms) SELECT `companies`.* FROM `companies` WHERE `companies`.`name` = 'ΕΛΕ�Α ΤΖΙΟΒΑ�Η' LIMIT 1
SQL (0.0ms) BEGIN
SQL (0.0ms) SELECT 1 FROM `companies` WHERE `companies`.`name` = 'ΕΛΕ�Α ΤΖΙΟΒΑ�Η' AND `companies`.`country_id` = 10 AND (`companies`.`name` = BINARY 'ΕΛΕ�Α ΤΖΙΟΒΑ�Η') LIMIT 1
AREL (0.0ms) INSERT INTO `companies` (`email`, `phone`, `country_id`, `created_at`, `address`, `contact`, `fax`, `updated_at`, `name`) VALUES ('[email protected]', '0035723833780', 10, '2011-05-25 13:20:45', 'Î」ΑΦÎ�ΗΣ 11-13 5314 AMMOHOSTOS', 'MRS GEORGIA MASIA', '0035723833177', '2011-05-25 13:20:45', 'ΕΛΕÎ�Α ΤΖΙΟΒΑÎ�Η')
SQL (31.2ms) COMMIT
SQL (0.0ms) BEGIN
SQL (0.0ms) ROLLBACK
Country Load (0.0ms) SELECT `countries`.* FROM `countries` WHERE `countries`.`code` = 'cy' LIMIT 1
Company Load (0.0ms) SELECT `companies`.* FROM `companies` WHERE `companies`.`name` = 'EURES ADVISOR , MINAS HADJICONSTANTI' LIMIT 1
SQL (0.0ms) BEGIN
SQL (0.0ms) SELECT 1 FROM `companies` WHERE `companies`.`name` = 'EURES ADVISOR , MINAS HADJICONSTANTI' AND `companies`.`country_id` = 10 AND (`companies`.`name` = BINARY 'EURES ADVISOR , MINAS HADJICONSTANTI') LIMIT 1
AREL (0.0ms) INSERT INTO `companies` (`email`, `phone`, `country_id`, `created_at`, `address`, `contact`, `fax`, `updated_at`, `name`) VALUES ('[email protected]', '00357 24 805328', 10, '2011-05-25 13:21:01', 'P.O.BOX 40136 6301 , ΛΑΡ�ΑΚΑ', 'MINAS HADJICONSTANTI', '00357 24 304532', '2011-05-25 13:21:01', 'EURES ADVISOR , MINAS HADJICONSTANTI')
SQL (62.4ms) COMMIT
SQL (0.0ms) BEGIN
SQL (1.0ms) ROLLBACK
Country Load (0.0ms) SELECT `countries`.* FROM `countries` WHERE `countries`.`code` = 'it' LIMIT 1
Company Load (0.0ms) SELECT `companies`.* FROM `companies` WHERE `companies`.`name` = 'SAILER ANDREAS' LIMIT 1
SQL (0.0ms) BEGIN
SQL (0.0ms) SELECT 1 FROM `companies` WHERE `companies`.`name` = 'SAILER ANDREAS' AND `companies`.`country_id` = 8 AND (`companies`.`name` = BINARY 'SAILER ANDREAS') LIMIT 1
AREL (0.0ms) INSERT INTO `companies` (`email`, `phone`, `country_id`, `created_at`, `address`, `contact`, `fax`, `updated_at`, `name`) VALUES ('[email protected]', '+39-0471-932632', 8, '2011-05-25 13:21:18', 'A.Duerer-Str. 20 - Via Duerer 20 39100-Bozen - Bolzano', 'Giulia Morello', '+39-0471-932691', '2011-05-25 13:21:18', 'SAILER ANDREAS')
SQL (30.0ms) COMMIT
SQL (0.0ms) BEGIN
AREL (0.0ms) INSERT INTO `vacancies` (`country_id`, `pes_id`, `title`, `region_code`, `isco_code`, `created_at`, `updated_at`, `description`, `company_id`, `nace_code`) VALUES (8, 64, 'ricerca di ingegnere e architetto per studio di ingegneria', 'R3311', 4190, '2011-05-25 13:21:24', '2011-05-25 13:21:24', 'Studio di ingegneria cerca, anche per subito, un ingegnere e un architetto, per la progettazione architettonica e strutturale, stima e contabilità di opere, il tutto nel settore delle costruzioni civili e delle opere speciali . E\' richiesta ottima conoscenza di autocad, pacchetto office e programmi di strutture e contabilità . Sede di lavoro: Bolzano centro: si chiede gentilmente che venga inviato un curriculum professionale con foto allegata.', 1, 74)
SQL (62.4ms) COMMIT
人們似乎對這些國家&企業走出去的比我想有很多更選擇查詢。我想知道是否可以對代碼進行優化以減少這些查詢。
哇有趣的材料。我會毫不猶豫地看看這個,看看它是否更快。有沒有關於這個地方的詳細信息?我的意思是緩存在RoR? – 2011-05-25 17:33:25
有一種名爲'memoize'的方法,它是由Rails添加的,可以讓你編寫像這樣的輔助方法,儘管內部並不比你在這裏看到的複雜得多。緩存時要小心扔掉不需要的內容。例如,您可能需要在完成插入後重置緩存以釋放由這些緩存實例佔用的內存。 – tadman 2011-05-25 18:18:24
謝謝你。我也gonne調查memcached,看看這可以帶來soem的幫助 – 2011-05-26 06:51:52